You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors. You may instantiate any of the exceptions in Table A-26, but interpreter-generated exceptions are usually of type Error.
Exception Object |
Description |
---|---|
Error |
Generic exception |
EvalError |
Thrown when eval() is used incorrectly. |
RangeError |
Thrown when a number exceeds the maximum allowable range. |
ReferenceError |
Thrown on the rare occasion that an invalid reference is used. |
SyntaxError |
Thrown when some sort of syntax error has occurred at runtime. Note that “real” JavaScript syntax errors are not catchable. |
TypeError |
Thrown when an operand has an unexpected type. |
URIError |
Thrown when one of Global's URI-related functions is used incorrectly. |
You can invoke exceptions directly using throw.
throw: value;
The value can be any value, but is generally an Error instance.
Exceptions can be handled with the common try/catch/finally block structure.
try { statementsToTry } catch ( e ) { catchStatements } finally { finallyStatements }
The try block must be followed by either exactly one catch block or one finally block (or one of both). When an exception occurs in the catch block, the exception is placed in e and the catch block is executed. The finally block executes unconditionally after try/catch.