13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Handling errors<br />

try<br />

{<br />

try<br />

{<br />

trace(">");<br />

throw new ApplicationError("some error which will be rethrown");<br />

}<br />

catch (error:ApplicationError)<br />

{<br />

trace("> " + error);<br />

trace(">");<br />

throw error;<br />

}<br />

catch (error:Error)<br />

{<br />

trace("> " + error);<br />

}<br />

}<br />

catch (error:ApplicationError)<br />

{<br />

trace("> " + error);<br />

}<br />

The output from the previous snippet would be the following:<br />

><br />

> ApplicationError: some error which will be rethrown<br />

><br />

> ApplicationError: some error which will be rethrown<br />

The nested try block throws a custom ApplicationError error that is caught by the subsequ<strong>en</strong>t catch block. This<br />

nested catch block can try to handle the error, and if unsuccessful, throw the ApplicationError object to the <strong>en</strong>closing<br />

try..catch block.<br />

Creating custom error classes<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

You can ext<strong>en</strong>d one of the standard Error classes to create your own specialized error classes in ActionScript. There<br />

are a number of reasons to create your own error classes:<br />

To id<strong>en</strong>tify specific errors or groups of errors that are unique to your application.<br />

For example, take differ<strong>en</strong>t actions for errors thrown by your own code, in addition to those errors trapped by a<br />

Flash runtime. You can create a subclass of the Error class to track the new error data type in try..catch blocks.<br />

To provide unique error display capabilities for errors g<strong>en</strong>erated by your application.<br />

For example, you can create a new toString() method that formats your error messages in a certain way. You can<br />

also define a lookupErrorString() method that takes an error code and retrieves the proper message based on<br />

the user’s language prefer<strong>en</strong>ce.<br />

A specialized error class must ext<strong>en</strong>d the core ActionScript Error class. Here is an example of a specialized AppError<br />

class that ext<strong>en</strong>ds the Error class:<br />

Last updated 6/6/2012<br />

62

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!