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

There are differ<strong>en</strong>t ways you can approach error handling in your application. The following list summarizes the three<br />

major options for handling errors:<br />

Use try..catch..finally statem<strong>en</strong>ts. These statem<strong>en</strong>ts catch synchronous errors as they occur. You can nest<br />

your statem<strong>en</strong>ts into a hierarchy to catch exceptions at various levels of code execution. For more information, see<br />

“Using try..catch..finally statem<strong>en</strong>ts” on page 58.<br />

Create your own custom error objects. You can use the Error class to create your own custom error objects to track<br />

specific operations in your application that are not covered by built-in error types. Th<strong>en</strong> you can use<br />

try..catch..finally statem<strong>en</strong>ts on your custom error objects. For more information see “Creating custom<br />

error classes” on page 62.<br />

Write ev<strong>en</strong>t list<strong>en</strong>ers and handlers to respond to error ev<strong>en</strong>ts. By using this strategy, you can create global error<br />

handlers that let you handle similar ev<strong>en</strong>ts without duplicating much code in try..catch..finally blocks. You<br />

are also more likely to catch asynchronous errors using this approach. For more information, see “Responding to<br />

error ev<strong>en</strong>ts and status” on page 63.<br />

Working with the debugger versions of Flash runtimes<br />

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

Adobe provides developers with special editions of the Flash runtimes to assist debugging efforts. You obtain a copy<br />

of the debugger version of Flash Player wh<strong>en</strong> you install Adobe Flash Professional or Adobe Flash Builder. You also<br />

obtain a utility for the debugging of Adobe AIR applications, which is called ADL, wh<strong>en</strong> you install either of those<br />

tools, or as part of the Adobe AIR SDK.<br />

There is a notable differ<strong>en</strong>ce in how the debugger versions and the release versions of Flash Player and Adobe AIR<br />

indicate errors. The debugger versions shows the error type (such as a g<strong>en</strong>eric Error, IOError, or EOFError), error<br />

number, and a human-readable error message. The release versions shows only the error type and error number. For<br />

example, consider the following code:<br />

try<br />

{<br />

tf.text = myByteArray.readBoolean();<br />

}<br />

catch (error:EOFError)<br />

{<br />

tf.text = error.toString();<br />

}<br />

If the readBoolean() method throws an EOFError in the debugger version of Flash Player, the following message<br />

displays in the tf text field: “EOFError: Error #2030: End of file was <strong>en</strong>countered.”<br />

The same code in a release version of Flash Player or Adobe AIR would display the following text: “EOFError: Error<br />

#2030.”<br />

Note: The debugger players broadcast an ev<strong>en</strong>t named "allComplete"; avoid creating custom ev<strong>en</strong>ts with the name<br />

“allComplete”. Otherwise, you will <strong>en</strong>counter unpredictable behavior wh<strong>en</strong> debugging.<br />

To keep resources and size to a minimum in the release versions, error message strings are not pres<strong>en</strong>t. You can look<br />

up the error number in the docum<strong>en</strong>tation (the app<strong>en</strong>dixes of the ActionScript 3.0 Refer<strong>en</strong>ce for the Adobe Flash<br />

Platform) to correlate to an error message. Alternatively, you can reproduce the error using the debugger versions of<br />

Flash Player and AIR to see the full message.<br />

Last updated 6/6/2012<br />

57

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

Saved successfully!

Ooh no, something went wrong!