JavaScript:有没有办法让 Chrome 解决所有错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2233339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: Is there a way to get Chrome to break on all errors?
提问by avernet
I am looking for an equivalent in Chrome to the "break on all errors" functionality of Firebug. In the Scripts tab, Chrome has a "pause on all exceptions", but this is not quite the same as breaking on all errors.
我正在 Chrome 中寻找与 Firebug 的“中断所有错误”功能相同的功能。在脚本选项卡中,Chrome 有一个“暂停所有异常”,但这与中断所有错误并不完全相同。
For instance, when loading a page with the following code, I would like Chrome to break on the line foo.bar = 42. Instead, even when enabling the "Pause on all exceptions", I don't get the expected result.
例如,在加载包含以下代码的页面时,我希望 Chrome 断线foo.bar = 42。相反,即使启用“在所有异常时暂停”,我也没有得到预期的结果。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
function doError() {
foo.bar = 42;
}
window.onload = function() {
try {
doError();
} catch (e) {
console.log("Error", e);
}
}
</script>
</head>
<body>
</body>
</html>
You can try the code pasted above on this pageor using this jsFiddle.
采纳答案by Brian Arnold Sinclair
Edit: The original link I answered with is now invalid.The newer URL would be https://developers.google.com/web/tools/chrome-devtools/javascript/add-breakpoints#exceptionsas of 2016-11-11.
编辑:我回答的原始链接现在无效。截至 2016 年 11 月 11 日,较新的 URL 将是https://developers.google.com/web/tools/chrome-devtools/javascript/add-breakpoints#exceptions。
I realize this question has an answer, but it's no longer accurate. Use the link above ^
我意识到这个问题有答案,但它不再准确。使用上面的链接^
(link replaced by edited above) - you can now set it to break on all exceptions or just unhandled ones. (Note that you need to be in the Sourcestab to see the button.)
(链接由上面编辑替换)-您现在可以将其设置为在所有异常或未处理的异常上中断。(请注意,您需要在“来源”选项卡中才能看到该按钮。)
Chrome's also added some other really useful breakpoint capabilities now, such as breaking on DOM changes or network events.
Chrome 现在还添加了一些其他非常有用的断点功能,例如中断 DOM 更改或网络事件。
Normally I wouldn't re-answer a question, but I had the same question myself, and I found this now-wrong answer, so I figured I'd put this information in here for people who came along later in searching. :)
通常我不会重新回答问题,但我自己也有同样的问题,我发现这个现在是错误的答案,所以我想我会把这些信息放在这里供后来搜索的人使用。:)
回答by Maxence
I got trouble to get it so I post pictures showing different options:
我在获取它时遇到了麻烦,所以我发布了显示不同选项的图片:
Chrome 75.0.3770.142[29 July 2018]
Chrome 75.0.3770.142[2018 年 7 月 29 日]
Very very similar UI since at least Chrome 38.0.2125.111 [11 December 2014]
至少自 Chrome 38.0.2125.111 [2014 年 12 月 11 日] 以来非常相似的用户界面
In tab Sources:
在选项卡中Sources:
When button is activated, you can Pause On Caught Exceptionswith the checkbox below:

当按钮被激活时,您可以Pause On Caught Exceptions使用下面的复选框:

Previous versions
之前的版本
铬合金 32.0.1700.10232.0.1700.102[2014 年 2 月 3 日]









回答by Brad Parks
This is now supported in Chrome by the "Pause on all exceptions" button.
Chrome 现在通过“暂停所有例外”按钮来支持这一点。
To enable it:
要启用它:
- Go to the "Sources" tab in Chrome Developer Tools
- Click the "Pause" button at the bottom of the window to switch to "Pause on all exceptions mode".
- 转到 Chrome 开发者工具中的“来源”标签
- 单击窗口底部的“暂停”按钮切换到“在所有例外模式下暂停”。
Note that this button has multiple states. Keep clicking the button to switch between
请注意,此按钮有多种状态。一直点击按钮切换
- "Pause on all exceptions" - the button is colored light blue
- "Pause on uncaught exceptions", the button is colored purple.
- "Dont pause on exceptions" - the button is colored gray
- “暂停所有例外” - 按钮为浅蓝色
- “暂停未捕获的异常”,按钮为紫色。
- “不要暂停异常” - 按钮为灰色
回答by Matthew Crumley
Just about any error will throw an exceptions. The only errors I can think of that wouldn't work with the "pause on exceptions" option are syntax errors, which happen before any of the code gets executed, so there's no place to pause anyway and none of the code will run.
几乎任何错误都会引发异常。我能想到的唯一一个不适用于“异常暂停”选项的错误是语法错误,它发生在任何代码执行之前,因此无论如何都没有暂停的地方,并且没有任何代码会运行。
Apparently, Chrome won't pause on the exception if it's inside a try-catch block though. It only pauses on uncaught exceptions. I don't know of any way to change it.
显然,如果它在 try-catch 块内,Chrome 不会暂停异常。它只暂停未捕获的异常。我不知道有什么办法可以改变它。
If you just need to know what line the exception happened on (then you could set a breakpoint if the exception is reproducible), the Errorobject given to the catch block has a stackproperty that shows where the exception happened.
如果您只需要知道异常发生在哪一行(如果异常是可重现的,那么您可以设置断点),Error提供给 catch 块的对象具有一个stack显示异常发生位置的属性。
回答by avernet
Unfortunately, it the Developer Tools in Chrome seem to be unable to "stop on all errors", as Firebug does.
不幸的是,Chrome 中的开发人员工具似乎无法像 Firebug 那样“停止所有错误”。

