This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.

Monday, August 22, 2005

AJAX: How to Catch Evil Errors with OnError

Over on the Ajaxian blog they mention some evil code that was squelching exceptions (i.e. it was doing a try/catch and then doing nothing in the catch). I've run into this issue myself, having things fail mysteriously. One way to catch these kinds of problems is to use window.onerror (another reference):

var errorHandler = function(msg, url, linenumber) {
alert("Error: " + msg + " for " + url + " at " + linenumber);
};

window.onerror = errorHandler;
Now, whenever an error occurs, the error handler will get an error message, the URL that is the source of the error, and a line number! Window.onerror works in Firefox and IE, but I believe that it fails in Safari (continuing my general frustration with Safari as a browser).


Comments:
My testing shows onerror only works on uncaught exceptions. So if something is trapped in try{} and an exception is not further thrown, your onerror handler will not see it. But this is how it should work so this is a good thing IMHO.

Now if only Safari and Opera would support this. I've been Googling about this and am dismayed that these two have chosen not support it, regardless of whether it is a "standard" or not!
 
As far as I can tell, this is still the case, and still frustrating...

Any word on whether it might someday change?
 
Post a Comment



Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]