User Guide

Statements 231
}]
Encloses a block of code in which an error can occur, and then respond to the error. If any
code in the
try code block throws an error (using the throw statement), control passes to the
catch block, if one exists, and then to the finally code block, if one exists. The finally
block is always executed, regardless of whether an error was thrown. If code in the
try block
doesn't throw an error (that is, if the
try block completes normally), then the code in the
finally block is still executed. The finally block is executed even if the try block exits
using a
return statement.
A
try block must be followed by a catch block, a finally block, or both. A single try block
can have multiple
catch blocks but only one finally block. You can nest try blocks as
many levels deep as necessary.
The
error parameter specified in a catch handler must be a simple identifier such as e or
theException or x. The variable in a catch handler can also be typed. When used with
multiple
catch blocks, typed errors let you catch multiple types of errors thrown from a single
try block.
If the exception thrown is an object, the type matches if the thrown object is a subclass of the
specified type. If an error of a specific type is thrown, the
catch block that handles the
corresponding error is executed. If an exception that is not of the specified type is thrown, the
catch block is not executed and the exception is automatically thrown out of the try block to
a
catch handler that matches it.
If an error is thrown within a function, and the function does not include a
catch handler,
then the ActionScript interpreter exits that function, as well as any caller functions, until a
catch block is found. During this process, finally handlers are called at all levels.
Availability: ActionScript 1.0; Flash Player 7
Parameters
error:Object - The expression thrown from a throw statement, typically an instance of the
Error class or one of its subclasses.
Example
The following example shows how to create a
try..finally statement. Because code in the
finally block is guaranteed to be executed, it is typically used to perform any necessary
clean-up after a
try block is executed. In the following example, setInterval()calls a
function every 1000 millisecond (1 second). If an error occurs, an error is thrown and is
caught by the
catch block. The finally block is always executed whether or not an error
occurs. Because
setInterval() is used, clearInterval() must be placed in the finally
block to ensure that the interval is cleared from memory: