Pages

Monday, January 31, 2011

Javascript - Catch Unhandled Exceptions

Have you ever thought to manage all exceptions at client side? if you use a simple line of code, like the one presented below, you can get an simple message and direct you to the right way to solve the script problem.










The window.onerror is an event handler for errors events sent to the window, catching all Unhandled Exceptions. But this approach has the problem that only works on IE or Gecko based browsers. It's a damn limitation for sure...

BTW, in order to test the onerror event, use a simple code line like "color[2]" without any initializing letting the script get error, then you'll see the alert window.



References:
https://developer.mozilla.org/En/DOM/Window.onerror
http://stackoverflow.com/questions/339580/does-javascript-fire-an-event-for-unhandled-uncaught-exceptions
http://stackoverflow.com/questions/1008692/window-onerror-not-firing-in-firefox