Section 14.7. Error Handling


14.7. Error Handling

The onerror property of a Window object is special. If you assign a function to this property, the function is invoked whenever a JavaScript error occurs in that window: the function you assign becomes an error handler for the window. (Note that you ought to be able to simply define a global function named onerror( ), as this should be equivalent to assigning a function to the onerror property of a Window object. Defining a global onerror( ) function does not work in IE, however.)

Three arguments are passed to an error handler when a JavaScript error occurs. The first is a message describing the error. This may be something like "missing operator in expression", "self is read-only", or "myname is not defined". The second argument is a string that contains the URL of the document containing the JavaScript code that caused the error. The third argument is the line number within the document where the error occurred. An error handler can use these arguments for any purpose; a typical error handler might display the error message to the user, log it somewhere, or force the error to be ignored. Prior to JavaScript 1.5, the onerror handler can also be used as a poor substitute for TRy/catch (see Chapter 6) exception handling.

In addition to those three arguments, the return value of the onerror handler is significant. If the onerror handler returns TRue, it tells the browser that the handler has handled the error and that no further action is necessaryin other words, the browser should not display its own error message.

Browsers have changed the way they handle JavaScript errors over the years. When JavaScript was new, and web browsers were young, it was typical for the browser to pop up a dialog box every time a JavaScript error occurred. These dialogs are useful to developers but disruptive to end users. To prevent an end user from ever seeing an error dialog on a production web site (many web sites cause JavaScript errors, at least in some browsers), you can simply define an error handler that handles all errors silently:

 // Don't bother the user with error reports window.onerror = function( ) { return true; } 

As poorly written and incompatible JavaScript code proliferated on the Web, and JavaScript errors became commonplace, web browsers began to log errors unobtrusively. This is better for end users but more difficult for JavaScript developers, who (in Firefox, for example) have to open up a JavaScript Console window to see whether errors have occurred. To simplify matters during code development, you might define an error handler like this:

 // Display error messages in a dialog box, but never more than 3 window.onerror = function(msg, url, line) {     if (onerror.num++ < onerror.max) {         alert("ERROR: " + msg + "\n" + url + ":" + line);         return true;     } } onerror.max = 3; onerror.num = 0; 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net