Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Javascript fire an event for unhandled/uncaught exceptions?

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

like image 522
Nathaniel Reinhart Avatar asked Dec 04 '08 04:12

Nathaniel Reinhart


People also ask

Which event is used for unhandled exceptions?

The UnhandledException event is raised for unhandled exceptions thrown in other threads. Starting with Microsoft Visual Studio 2005, the Visual Basic application framework provides another event for unhandled exceptions in the main application thread.

What is an uncaught exception JavaScript?

According to the Mozilla website for developer documents, “the TypeError object represents an error when a value is not of the expected type.” Uncaught means that the error was not caught in the catch part of the try-catch block.

What happens if an exception goes uncaught?

If an exception is not caught, it is intercepted by a function called the uncaught exception handler. The uncaught exception handler always causes the program to exit but may perform some task before this happens. The default uncaught exception handler logs a message to the console before it exits the program.

Are uncaught exceptions ignored by JVM?

Method invoked when the given thread terminates due to the given uncaught exception. Any exception thrown by this method will be ignored by the Java Virtual Machine.


1 Answers

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.

like image 56
Dave Dopson Avatar answered Sep 23 '22 15:09

Dave Dopson