ADOBE FLEX 3
Developer Guide
175
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 or Adobe AIR. 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);
}
}
The following shows an example of using AppError in your project:
try
{
throw new AppError("Encountered Custom AppError", 29);
}
catch (error:AppError)
{
trace(error.errorID + ": " + error.message)
}
Note: If you want to override the Error.toString() method in your subclass, you need to give it one ...(rest)
parameter. The ECMAScript (ECMA-262) edition 3 language specification defines the
Error.toString() method
that way, and ActionScript 3.0 defines it the same way for backward compatibility with that specification. Therefore,
when you override the
Error.toString() method, you must match the parameters exactly. You will not want to pass
any parameters to your
toString() method at run time, because those parameters are ignored.
Responding to error events and status
One of the most noticeable improvements to error handling in ActionScript 3.0 is the support for error event
handling for responding to asynchronous run-time errors. (For a definition of asynchronous errors, see “Types of
errors” on page 167.)
ADOBE FLEX 3
Developer Guide
176
You can create event listeners and event handlers to respond to the error events. Many classes dispatch error events
the same way they dispatch other events. For example, an instance of the XMLSocket class normally dispatches three
types of events:
Event.CLOSE, Event.CONNECT, and DataEvent.DATA. However, when a problem occurs, the
XMLSocket class can dispatch the
IOErrorEvent.IOError or the SecurityErrorEvent.SECURITY_ERROR. For
more information about event listeners and event handlers, see Handling events” on page 227.
Error events fit into one of two categories:
Error events that extend the ErrorEvent class
The flash.events.ErrorEvent class contains the properties and methods for managing run-time errors related
to networking and communication operations. The AsyncErrorEvent, IOErrorEvent, and SecurityError-
Event classes extend the ErrorEvent class. If you’re using the debugger version of Flash Player or Adobe AIR,
a dialog box will inform you at run-time of any error events without listener functions that the player
encounters.
Status-based error events
The status-based error events are related to the
netStatus and status properties of the networking and
communication classes. If Flash Player or Adobe AIR encounters a problem when reading or writing data,
the value of the
netStatus.info.level or status.level properties (depending on the class object you’re
using) is set to the value
"error". You respond to this error by checking if the level property contains the
value
"error" in your event handler function.
Working with error events
The ErrorEvent class and its subclasses contain error types for handling errors dispatched by Flash Player and Adobe
AIR as they try to read or write data.
The following example uses both a
try..catch statement and error event handlers to display any errors detected
while trying to read a local file. You can add more sophisticated handling code to provide a user with options or
otherwise handle the error automatically in the places indicated by the comment “your error-handling code here”:
package
{
import flash.display.Sprite;
import flash.errors.IOError;
import flash.events.IOErrorEvent;
import flash.events.TextEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class LinkEventExample extends Sprite
{
private var myMP3:Sound;
public function LinkEventExample()
{
myMP3 = new Sound();
var list:TextField = new TextField();
list.autoSize = TextFieldAutoSize.LEFT;
list.multiline = true;
list.htmlText = "<a href=\"event:track1.mp3\">Track 1</a><br>";
list.htmlText += "<a href=\"event:track2.mp3\">Track 2</a><br>";

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.