When you work with synchronous run-time
errors, use the
try..catch..finally
statements
to catch errors. When a run-time error occurs, the Flash runtime
throws an exception, which means that it suspends normal execution
and creates a special object of type Error. The Error object is then
thrown to the first available
catch
block.
The
try
statement encloses statements that have
the potential to create errors. You always use the
catch
statement
with a
try
statement. If an error is detected in
one of the statements in the
try
statement block,
the
catch
statements that are attached to that
try
statement
run.
The
finally
statement encloses statements that
run whether 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 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 runs. 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 matches. 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
To correctly catch the ArgumentError, 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
{
throw new ArgumentError("I am an ArgumentError");
}
catch (error:ArgumentError)
{
trace("<ArgumentError> " + error.message);
}
catch (error:Error)
{
trace("<Error> " + error.message);
}
Several methods and properties in the ActionScript API throw
run-time errors if they encounter errors while they execute. For
example, the
close()
method in the Sound class
throws an IOError if the method is unable to close the audio stream,
as demonstrated in the following code:
var mySound:Sound = new Sound();
try
{
mySound.close();
}
catch (error:IOError)
{
// Error #2029: This URLStream object does not have an open stream.
}
As you become more familiar with the
ActionScript 3.0 Reference for the Adobe Flash
Platform
, you’ll notice which methods throw exceptions, as
detailed in each method’s description.