views:

163

answers:

1
function testFun() {
    onerror = function() { log("caught the error"); return true; };
    setTimeout(function() { throw "bad bad bad"; }, 300);
};

This is sample, code, but it demonstrates a problem. If I run this in FF, or IE7, it prints the sensible "caught the error" message (assume a reasonable 'log' function).

However if I debug the code in VS2008, the debugger stops on the throw with the message: 'Microsoft JScript runtime error: Exception thrown and not caught'. If I say 'continue' or 'ignore', the log message is not produced.

This is a problem since the real code I am working with is much larger than this, and I'll occasionally want to, you know, debug stuff. So two questions:

  1. Any know why and can I modify this behaviour with some flag I don't know about?
  2. Am I doing what I think I'm doing (setting the global 'onerror' handler) in this code? If not, what is the appropriate pattern for catching this type of error?

Note: There is no difference wrt this problem if I use window.onerror instead.

+1  A: 

According to this defining a global onerror function doesn't work in IE. They were probably talking about IE6 or earlier, so maybe MS have fixed it for IE7 - however I wouldn't expect this to just automatically flow through to the VS debugger.

At any rate, try using window.onerror = function rather than just onerror.

If that doesn't work, you'll have to use a try/catch block inside your timer function I guess.

PS: Get firefox and use firebug. the debugger (and everything else) is much better and nicer to use than the VS debugging

Orion Edwards