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 />

public class AppError ext<strong>en</strong>ds Error<br />

{<br />

public function AppError(message:String, errorID:int)<br />

{<br />

super(message, errorID);<br />

}<br />

}<br />

The following shows an example of using AppError in your project:<br />

try<br />

{<br />

throw new AppError("Encountered Custom AppError", 29);<br />

}<br />

catch (error:AppError)<br />

{<br />

trace(error.errorID + ": " + error.message)<br />

}<br />

Note: If you want to override the Error.toString() method in your subclass, give it one ...(rest) parameter. The<br />

ECMAScript language specification on which ActionScript 3.0 is based defines the Error.toString() method that way,<br />

and ActionScript 3.0 defines it the same way for backward compatibility. Therefore, wh<strong>en</strong> you override the<br />

Error.toString() method, match the parameters exactly. You do not want to pass any parameters to your<br />

toString() method at runtime, because those parameters are ignored.<br />

Responding to error ev<strong>en</strong>ts and status<br />

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

One of the most noticeable improvem<strong>en</strong>ts to error handling in ActionScript 3.0 is the support for error ev<strong>en</strong>t handling<br />

for responding to asynchronous errors while an application is running. (For a definition of asynchronous errors, see<br />

“Types of errors” on page 53.)<br />

You can create ev<strong>en</strong>t list<strong>en</strong>ers and ev<strong>en</strong>t handlers to respond to the error ev<strong>en</strong>ts. Many classes dispatch error ev<strong>en</strong>ts<br />

the same way they dispatch other ev<strong>en</strong>ts. For example, an instance of the XMLSocket class normally dispatches three<br />

types of ev<strong>en</strong>ts: Ev<strong>en</strong>t.CLOSE, Ev<strong>en</strong>t.CONNECT, and DataEv<strong>en</strong>t.DATA. However, wh<strong>en</strong> a problem occurs, the<br />

XMLSocket class can dispatch the IOErrorEv<strong>en</strong>t.IOError or the SecurityErrorEv<strong>en</strong>t.SECURITY_ERROR. For<br />

more information about ev<strong>en</strong>t list<strong>en</strong>ers and ev<strong>en</strong>t handlers, see “Handling ev<strong>en</strong>ts” on page 125.<br />

Error ev<strong>en</strong>ts fit into one of two categories:<br />

Error ev<strong>en</strong>ts that ext<strong>en</strong>d the ErrorEv<strong>en</strong>t class<br />

The flash.ev<strong>en</strong>ts.ErrorEv<strong>en</strong>t class contains the properties and methods for managing errors related to networking<br />

and communication operations in a running application. The AsyncErrorEv<strong>en</strong>t, IOErrorEv<strong>en</strong>t, and<br />

SecurityErrorEv<strong>en</strong>t classes ext<strong>en</strong>d the ErrorEv<strong>en</strong>t class. If you’re using the debugger version of a Flash runtime, a<br />

dialog box informs you at run-time of any error ev<strong>en</strong>ts without list<strong>en</strong>er functions that the player <strong>en</strong>counters.<br />

Status-based error ev<strong>en</strong>ts<br />

The status-based error ev<strong>en</strong>ts are related to the netStatus and status properties of the networking and<br />

communication classes. If a Flash runtime <strong>en</strong>counters a problem wh<strong>en</strong> reading or writing data, the value of the<br />

netStatus.info.level or status.level properties (dep<strong>en</strong>ding on the class object you’re using) is set to the<br />

value "error". You respond to this error by checking if the level property contains the value "error" in your<br />

ev<strong>en</strong>t handler function.<br />

Last updated 6/6/2012<br />

63

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

Saved successfully!

Ooh no, something went wrong!