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

4 comments:

  1. This is what exactly I was looking for. Now I can catch the unhandled error and inform the server about it (Ajax). The server will inform me by email. So I will come to know about the error. Helps me to identify and fix the errors.

    Thanks once again.

    ReplyDelete
  2. One more thing to add here is if you return false from this method instead of true, then the browser default handling (logging in browser javascript console etc) will also work.

    ReplyDelete
    Replies
    1. Happy to help :)

      regarding your second comment, I will certainly try it! Thanks!

      Cheers,

      AP

      Delete
  3. Companies have demonstrated that mobile applications are the best way to grow their customer base. Hence, to grow your business in this robust digital world, you need an equally robust mobile app to stay ahead of the game.
    Hire top-rated freelance mobile app developers to build interactive apps for your organization’s business goals.
    Eiliana.com freelancing portal houses top mobile app developers with a definite history of delivering high quality deliverables within the deadline.

    ReplyDelete