JavaScript: Is there a way to get Chrome to break on all errors?
Asked Answered
S

5

291

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.

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.

<!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 page or using this jsFiddle.

Sudduth answered 9/2, 2010 at 23:33 Comment(4)
What other kinds of errors are you thinking of?Haifa
Any error, say: a reference to an undefined variable (as in console.log(gaga)) or to an undefined property (as in window.foo.bar).Sudduth
Those should throw exceptions. The "pause on exceptions" option works for me. The only errors I can think of that wouldn't work are syntax errors, but those happen before the code gets executed.Haifa
Yes, I am trying this now, and it seems to break where the error is. I had done this a few days ago, and instead of breaking where the error is in the code, it was breaking where an exception caused by the error was being caught, way up the stack. Go figure!Sudduth
S
220

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#exceptions as of 2016-11-11.

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 Sources tab to see the button.)

Chrome's also added some other really useful breakpoint capabilities now, such as breaking on DOM changes or network events.

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. :)

Spew answered 3/1, 2011 at 19:13 Comment(5)
Brian, thank you, it is indeed working now. This feature was already available in February of last year, when I posted this, but it wasn't working properly, while it seems to be working fine now. Thanks for the feedback.Sudduth
The button referred to by your link doesn't seem to be there anymore (at least in the current dev channel Chrome release). Not sure how to do this again.Ataghan
Hi Andrey,I'm looking at Chrome Canary and I still see the button there. It's the fourth button from left, as shown at d.pr/i/DHwT - click once and it goes into Pause on All Exceptions, where a second click will take it to Pause on Uncaught Exceptions. A third click returns to the default of not pausing.Spew
This doesn't work when the script is blackboxed... Killed me several hours... Wish this could break on exceptions, blackboxed or not...Ideal
Even the amended answer with link is now wrong - there is no checkbox to "Pause on all exceptions" in the Chrome 69 I'm usingStempien
S
370

I got trouble to get it so I post pictures showing different options:

Chrome 111.0.5563.64 [updated on 14 March 2023]

Recent changed in Chrome moved in "Breakpoints" section [I discovered it on 14 March 2023]

enter image description here

Chrome 101.0.4951.64 [27 May 2022]

Very similar UI since at least Chrome 38.0.2125.111 [11 December 2014]

In tab Sources :

enter image description here

When button is activated, you can Pause On Caught Exceptions with the checkbox below: enter image description here


Previous versions

Chrome 32.0.1700.102 [03 feb 2014]

enter image description here

enter image description here enter image description here enter image description here

Chrome 27.0.1453.93 Stable

Debug options

Shemeka answered 26/6, 2013 at 15:39 Comment(3)
I have to praise Facebook for linking to this directly in their docs. +1Derinna
Also for people to note, the buttons will simply hide if the sidebar isn't expanded, with no indication they are there.Plexiglas
In the answer, I see 3 occurrences of "enter image description here" instead of screenshots. Fixing this would make the context of the post easier to understand, which is particularly important as this post is linked to as a reference from multiple sources: Facebook, localhost:19000/debugger-ui.Compliance
S
220

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#exceptions as of 2016-11-11.

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 Sources tab to see the button.)

Chrome's also added some other really useful breakpoint capabilities now, such as breaking on DOM changes or network events.

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. :)

Spew answered 3/1, 2011 at 19:13 Comment(5)
Brian, thank you, it is indeed working now. This feature was already available in February of last year, when I posted this, but it wasn't working properly, while it seems to be working fine now. Thanks for the feedback.Sudduth
The button referred to by your link doesn't seem to be there anymore (at least in the current dev channel Chrome release). Not sure how to do this again.Ataghan
Hi Andrey,I'm looking at Chrome Canary and I still see the button there. It's the fourth button from left, as shown at d.pr/i/DHwT - click once and it goes into Pause on All Exceptions, where a second click will take it to Pause on Uncaught Exceptions. A third click returns to the default of not pausing.Spew
This doesn't work when the script is blackboxed... Killed me several hours... Wish this could break on exceptions, blackboxed or not...Ideal
Even the amended answer with link is now wrong - there is no checkbox to "Pause on all exceptions" in the Chrome 69 I'm usingStempien
R
21

This is now supported in Chrome by the "Pause on all exceptions" button.

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".

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
Reamy answered 10/4, 2013 at 14:23 Comment(0)
H
1

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.

If you just need to know what line the exception happened on (then you could set a breakpoint if the exception is reproducible), the Error object given to the catch block has a stack property that shows where the exception happened.

Haifa answered 12/2, 2010 at 23:17 Comment(2)
Matthew, I "reproduced" something similar to the case I had (see the updated question). Somehow, I don't get the debugger to break on that error. Do you have any idea of why this wouldn't work with Chrome (it does with Firebug when enabling "break on all errors").Sudduth
@Mathhew, I created another answer saying this is impossible, and marked it as the answer. If you or someone else finds a solution, I will change the marker.Sudduth
S
1

Unfortunately, it the Developer Tools in Chrome seem to be unable to "stop on all errors", as Firebug does.

Sudduth answered 23/2, 2010 at 19:49 Comment(1)
Just click on the Pause Button ONE TIME (it will be in Blue color) and do not mark the option Pause On Caught Exceptions. That all, it will Pause only on errors.Charlean

© 2022 - 2024 — McMap. All rights reserved.