HTML and CSS Reference
In-Depth Information
Objective 2.3: Implement exception handling
That a program can deal with errors and unknown conditions is critical in any software
development. JavaScript is no exception and provides structured error-handling constructs to
deal with these situations.
Structured error handling in JavaScript is achieved with the try…catch…finally construct.
Good defensive programming also includes checking for null values, where appropriate, to
prevent errors. In addition to handling errors, code can raise custom errors as needed to send
error information back to a running program from custom objects or libraries.
This objective covers using the try…catch…finally construct, evaluating for the null
condition, and raising custom errors.
This objective covers how to:
Implement try-catch-finally blocks, including setting and responding to error
codes and throwing exceptions
Check for null values
Implementing try…catch…inally constructs
The try…catch…finally construct handles exceptions that occur during program processing. It
enables you to see what kind of error occurred and to do what's appropriate with it. If try…
catch…finally isn't implemented in the program, the errors would be treated as unhandled
and could cause the browser to crash or, at a minimum, display many annoying message
boxes to users, such as this one shown in Figure 2-3 that is caused from this code:
window.dosomeunsupportedmethod();
FIGURE 2-3 An unhandled exception error dialog box
 
 
 
Search WWH ::




Custom Search