7

What i try to accomplish is to register a global handler to catch all uncaught exceptions. Searching the web i only managed to find people pointing out window.onerror but this doesn't do the trick for me. Apparently window.onerror only gets called upon errors and not upon exceptions. Assume the following code:

    function windowError(message, url, line) {
        alert(message, url, line);
    }
    window.onerror=windowError;
    throw("uncaught");

The obviously uncaught exception won't trigger the windowError handler. (Using Firefox 3.6.3)

Any suggestions?

1
  • 1
    The above code works great on chrome v12. Cheers. Commented Jun 9, 2011 at 12:45

2 Answers 2

3

Errors are caught the same way as exceptions in javascript and in fact, in your example the message get's alerted (Firefox 3.6.3).

2
  • That is quite odd. It don't get any alert with Firefox 3.6.3.
    – Taloncor
    Commented Jun 2, 2010 at 17:44
  • I've tried your example on a simple html page and it does work. That's odd that on your Firefox is not 'alerting'... Have you tried on another browser? Commented Jun 2, 2010 at 18:16
2

As far as i know you'll need try/catch blocks to make this happen. That's sort of the point, that you need to know when to handle which kinds of errors.

1
  • found this post when googling for how to catch unhandeled javascript exceptions. You cant foresee all type of exceptions that will happen in a given code. window.onerror is a good way to at least display error. I have gone further and on my site i post the data in the window.onerror event to a webmethod on my server and logged the error to the app log.
    – Anders
    Commented Oct 31, 2012 at 15:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.