HTML and CSS Reference
In-Depth Information
Figure 7.20 Opera's Error Console.
7.2.2 Exception Handling with try/catch and throw
An exception is a runtime error that the program might encounter during its execution,
such as an undefined variable, an index value that is referenced but doesn't exist, a func-
tion that receives a bad parameter, and so on. Exception handlers make it possible to
catch errors and resolve them gracefully. By catching the exception and controlling the
error message, the program will be much nicer for an unwary user who is not used to
the kinds of error messages you see all the time. As of JavaScript1.5 exception handling
is now supported.
The try/catch Statements. You can enclose and test those parts of a program where
you expect potential problems in a try statement. If an exception occurs within the try
block, control will shift to the catch block. The catch block will contain statements to
clarify what went wrong. If there were no errors, the catch block will be ignored. See
Examples 7.12 and 7.13.
When an exception is thrown in the try block, the variable shown as e in catch(e) holds
the value of the type of exception (Table 7.1) that was thrown in the try block. You can
use this variable to get information about the exception that was thrown. (The variable
e can have any name and is local to the catch block.) You can use the name and message
properties with the catch variable to get the name of the exception and a message
explaining what caused the exception.
Search WWH ::

Custom Search