User Guide

266 Handling Errors
}
catch (error:Error)
{
trace("<< Error >> " + error);
}
}
catch (error:ApplicationError)
{
trace("<< catch >> " + error);
}
The output from the previous snippet would be the following:
<< try >>
<< catch >> ApplicationError: some error which will be rethrown
<< throw >>
<< catch >> ApplicationError: some error which will be rethrown
The nested try block throws a custom ApplicationError error that is caught by the
subsequent
catch block. This nested catch block can try to handle the error, and if
unsuccessful, throw the ApplicationError object to the enclosing
try..catch block.
Creating custom error classes
You can extend one of the standard Error classes to create your own specialized error classes in
ActionScript. There are a number of reasons to create your own error classes:
To identify specific errors or groups of errors that are unique to your application.
For example, you may want to take different actions for errors thrown by your own code,
in addition to those trapped by Flash Player. You can create a subclass of the Error class to
track the new error data type in
try..catch blocks.
To provide unique error display capabilities for errors generated by your application.
For example, you can create a new
toString() method that formats your error messages
in a certain way. You can also define a
lookupErrorString() method that takes an error
code and retrieves the proper message based on the user’s language preference.
A specialized error class must extend the core ActionScript Error class. Here is an example of a
specialized AppError class that extends the Error class:
public class AppError extends Error
{
public function AppError(message:String, errorID:int)
{
super(message, errorID);
}
}