Monday, April 23, 2012

Javascript global error handling


I would like to catch every undefined function error thrown. Is there a global error handling facility in Javascript? The use case is catching function calls from flash that are not defined.



Source: Tips4all

2 comments:

  1. Does this help you:

    <script type="text/javascript">
    window.onerror = function() {
    alert("Error caught");
    };

    xxx();
    </script>


    I'm not sure how it handles Flash errors though...

    Update: it doesn't work in Opera, but I'm hacking Dragonfly right now to see what it gets. Suggestion about hacking Dragonfly came from this question:

    http://stackoverflow.com/questions/645840/mimic-window-onerror-in-opera-using-javascript

    ReplyDelete
  2. sophisticated error handling

    If your error handling is very sophisticated and therefore might throw an error itself, it is useful to add a flag indicating if you are already in "errorHandling-Mode". Like so:

    var appIsHandlingError = false;

    window.onerror = function() {
    if (!appIsHandlingError) {
    appIsHandlingError = true;
    handleError();
    }
    };

    function handleError() {
    // graceful error handling
    // if successful: appIsHandlingError = false;
    }


    Otherwise you could find yourself in an infinite loop.

    ReplyDelete