|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Creating Error HandlersIn some cases, there may be times when an error message is unavoidable and, in a large JavaScript application, errors are bound to happen. Your scripts can respond to errors in a friendlier way using error handlers. Using the onerror PropertyYou can set up an error handler by assigning a function to the onerror property of the window object. When an error occurs in a script in the document, the browser calls the function you specify instead of the normal error dialog. For example, these statements set up a function that displays a simple message when an error occurs: function errmsg(message,url,line) {
alert("There wasn"t an error. Nothing to see here.");
return true;
}
window.onerror=errmsg;
These statements define a function, errmsg(), which handles errors by displaying a simple dialog. The last statement assigns the errmsg() function to the window.onerror property. The return true; statement tells the browser that this function has handled the error, and prevents the standard error dialog from being displayed. If you use return false; instead, the standard error dialog will be displayed after your function exits. By the Way You can't define an onError event handler in HTML. You must define it using the window.onerror property as shown here. Displaying Information About the ErrorWhen the browser calls your error-handling function, it passes three parameters: the error message, the URL of the document where the error happened, and the line number. The simple error handler in the previous example didn't use these values. You can create a more sophisticated handler that displays the information. Did you Know? As usual, you can download this hour's examples from this book's website. Listing 16.1 shows a complete example including an enhanced errmsg() function. This version displays the error message, URL, and line number in a dialog box. Listing 16.1. Handling Errors with a JavaScript Function
This example includes a button with a nonsensical event handler. To test the error handler, click the button to generate an error. Figure 16.2 shows the example in action in Internet Explorer with the alert message displayed. Figure 16.2. The error-handler example in action.
Did you Know? If you try to use an error handler and still get system error messages, make sure there isn't a syntax error in your error handler itself. Using try and catchA more modern way of handling errors, supported by the latest browsers, is the try and catch keywords. To use it, include the try keyword, then a block of code (within braces) that might cause an error, then the catch keyword, and a block of code to handle the error: try {
DoThis();
} catch(err) {
alert(err.description);
}
The TRy block of code always executes. If it generates an error, the catch block is executed. If there is no error, the catch block does not execute. The error-handling code is passed an argument (err in the example) indicating the type of error. This is an object with properties including name (the error name) and description (a description of the error). Did you Know? Handling errors with try and catch is a good way to deal with browser-specific code that might cause errors when run in the wrong browser. See the next hour for an example that uses TRy and catch to create a cross-browser AJAX function. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |