42

I'm looking to log unhandled javascript exceptions. Is there an event that fires when an exception isn't caught? I'm looking to catch the exceptions before they cause javascript errors in the browser, but I'd rather not run my entire application inside of a try/catch. Any help would be appreciated. Thanks!

Update: tvanfosson pointed out onerror as a possibility. It is not part of a spec and is only available in IE or Gecko based browsers.

For more information - http://books.google.com/books?id=tKszhx-XkzYC&pg=PA386&lpg=PA386&dq=safari+onerror+javascript&source=web&ots=gQaGbpUnjG&sig=iBCtOQs0aH_EAzSbWlGa9v5flyo#PPA387,M1

OnError Support Table - http://www.quirksmode.org/dom/events/error.html

Mozilla's documentation - https://developer.mozilla.org/en/DOM/window.onerror

WebKit Bug Report - https://bugs.webkit.org/show_bug.cgi?id=8519

Nickolay
  • 31,095
  • 13
  • 107
  • 185
Nathaniel Reinhart
  • 1,173
  • 1
  • 8
  • 21

2 Answers2

40

Check out this Fiddle:

http://jsfiddle.net/xYsRA/1/

window.onerror = function (msg, url, line) {
    console.log("Caught[via window.onerror]: '" + msg + "' from " + url + ":" + line);
    return true; // same as preventDefault
};

window.addEventListener('error', function (evt) {
    console.log("Caught[via 'error' event]:  '" + evt.message + "' from " + evt.filename + ":" + evt.lineno);
    console.log(evt); // has srcElement / target / etc
    evt.preventDefault();
});


throw new Error("Hewwo world.  I crash you!!!");

throw new Error("Hewwo world.  I can only crash you once... :(");

Which prints:

Caught[via window.onerror]: 'Uncaught Error: Hewwo world.  I crash you!!!' from http://fiddle.jshell.net/xYsRA/1/show/:32 fiddle.jshell.net:21
Caught[via 'error' event]:  'Uncaught Error: Hewwo world.  I crash you!!!' from http://fiddle.jshell.net/xYsRA/1/show/:32 fiddle.jshell.net:26
ErrorEvent {lineno: 32, filename: "http://fiddle.jshell.net/xYsRA/1/show/", message: "Uncaught Error: Hewwo world.  I crash you!!!", clipboardData: undefined, cancelBubble: false…}
 fiddle.jshell.net:27\

Notes:

  • If you remove the "return true" / "evt.preventDefault()" lines, then after the error is logged, it will print on the JS console in the normal way.

  • Contrary to statements made above, window.onerror worked in all the browsers I tested. However, the addEventListener method is probably better anyways and provides richer semantics.

Dave Dopson
  • 41,600
  • 19
  • 95
  • 85
  • Here's an updated fiddle that removes a dependency on a library that is no longer available: http://jsfiddle.net/xYsRA/103/ – Aaron Mason Sep 27 '18 at 23:06
11

Try using an onerror handler. Docs from MDN. This will allow you to do something when an error is detected, but probably won't let you continue in a graceful way that a try/catch block would. Be aware that are undoubtedly differences between browsers in how this is handled.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 7
    Here is a standard warning against using w3schools.com as a reference. See http://www.w3fools.com for more information. – L0j1k Oct 03 '14 at 21:14
  • 5
    @L0j1k I typically don't use it now as a resource, but it's interesting that even w3fools.com recognized they have improved. "W3Schools still has issues but they have at least worked on the primary concern developers had. For many beginners, W3Schools has structured tutorials and playgrounds that offer a decent learning experience. However, it would be a mistake to continue your education without learning from more reputable sources, so when you're ready to level up, move on." – tvanfosson Oct 03 '14 at 21:26
  • Link is now broken – cletus Sep 15 '16 at 20:48
  • 1
    Better yet, please post your example to be contained in the question rather than sharing a link that may change in 5 years. – Umopepisdn Jan 26 '17 at 02:46