User Guide

262 Handling Errors
The finally statement encloses statements that will execute whether or not an error occurs
in the
try block. If there is no error, the statements within the finally block execute after
the
try block statements complete. If there is an error, the appropriate catch statement
executes first, followed by the statements in the
finally block.
The following code demonstrates the syntax for using the
try..catch..finally statements:
try
{
// some code that could throw an error
}
catch (err:Error)
{
// code to react to the error
}
finally
{
// Code that runs whether or not an error was thrown. This code can clean
// up after the error, or take steps to keep the application running.
}
Each catch statement identifies a specific type of exception that it handles. The catch
statement can specify only error classes that are subclasses of the Error class. Each
catch
statement is checked in order. Only the first
catch statement that matches the type of error
thrown will execute. In other words, if you first check the higher-level Error class and then a
subclass of the Error class, only the higher-level Error class will match. The following code
illustrates this point:
try
{
throw new ArgumentError("I am an ArgumentError");
}
catch (error:Error)
{
trace("<Error> " + error.message);
}
catch (error:ArgumentError)
{
trace("<ArgumentError> " + error.message);
}
The previous code displays the following output:
<Error> I am an ArgumentError
In order to correctly catch the ArgumentError, you need to make sure that the most specific
error types are listed first, and the more generic error types are listed later, as the following
code shows:
try
{