views:

621

answers:

3

How do I overwrite the global Exception handler in javascript so that it becomes the top level handler for all uncaught exceptions?

EDIT: window.onerror didnt work, code is:

<HTML>
 <HEAD>
<script language='javascript'>
    window.onerror = function (em, url, ln) {
        alert(em + ", " + url + ", " + ln);
        return false;
    }

    function fGo() {
        try
        {
            var a = b; // error here : b not defined
        }
        catch (e)
        {
            throw e;
        }
    }
</script>
 </HEAD>
 <BODY>
    <button onclick='fGo()'>GO</button>
 </BODY>
</HTML>

I'm testing on chrome, by the way. Developer console registers the uncaught exception, but the alert() in window.onerror does not appear.

A: 
window.onerror = function(errorMsg, url, lineNumber) {
    // code to run when error has occured on page
}
angest
A: 

Perhaps you're looking for window.onerror Not sure whether this is available on all browsers.

peller
It's not, notably on Safari, but it's a really good start.
quixoto
+3  A: 

Google Chrome doesn't support window.onerror, apparentlly it's not implemented in webkit.

What I did was to wrap JQuery using curring to create a proxy that always does a try...catch in the JQuery functions.

I use it in www.js-analytics.com, however the solution only holds for JQuery scripts.

Morten
That's right. And WebKit has recently made some advance on fixing that bug. It's not fixed yet, but here you have the link for updates: https://bugs.webkit.org/show_bug.cgi?id=8519
Protron