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

Handling synchronous errors in an application<br />

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

The most common error handling is synchronous error-handling logic, where you insert statem<strong>en</strong>ts into your code to<br />

catch synchronous errors while an application is running. This type of error handling lets your application notice and<br />

recover from run-time errors wh<strong>en</strong> functions fail. The logic for catching a synchronous error includes<br />

try..catch..finally statem<strong>en</strong>ts, which literally try an operation, catch any error response from the Flash runtime,<br />

and finally execute some other operation to handle the failed operation.<br />

Using try..catch..finally statem<strong>en</strong>ts<br />

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

Wh<strong>en</strong> you work with synchronous run-time errors, use the try..catch..finally statem<strong>en</strong>ts to catch errors. Wh<strong>en</strong><br />

a run-time error occurs, the Flash runtime throws an exception, which means that it susp<strong>en</strong>ds normal execution and<br />

creates a special object of type Error. The Error object is th<strong>en</strong> thrown to the first available catch block.<br />

The try statem<strong>en</strong>t <strong>en</strong>closes statem<strong>en</strong>ts that have the pot<strong>en</strong>tial to create errors. You always use the catch statem<strong>en</strong>t<br />

with a try statem<strong>en</strong>t. If an error is detected in one of the statem<strong>en</strong>ts in the try statem<strong>en</strong>t block, the catch statem<strong>en</strong>ts<br />

that are attached to that try statem<strong>en</strong>t run.<br />

The finally statem<strong>en</strong>t <strong>en</strong>closes statem<strong>en</strong>ts that run whether an error occurs in the try block. If there is no error, the<br />

statem<strong>en</strong>ts within the finally block execute after the try block statem<strong>en</strong>ts complete. If there is an error, the<br />

appropriate catch statem<strong>en</strong>t executes first, followed by the statem<strong>en</strong>ts in the finally block.<br />

The following code demonstrates the syntax for using the try..catch..finally statem<strong>en</strong>ts:<br />

try<br />

{<br />

// some code that could throw an error<br />

}<br />

catch (err:Error)<br />

{<br />

// code to react to the error<br />

}<br />

finally<br />

{<br />

// Code that runs whether an error was thrown. This code can clean<br />

// up after the error, or take steps to keep the application running.<br />

}<br />

Each catch statem<strong>en</strong>t id<strong>en</strong>tifies a specific type of exception that it handles. The catch statem<strong>en</strong>t can specify only error<br />

classes that are subclasses of the Error class. Each catch statem<strong>en</strong>t is checked in order. Only the first catch statem<strong>en</strong>t<br />

that matches the type of error thrown runs. In other words, if you first check the higher-level Error class and th<strong>en</strong> a<br />

subclass of the Error class, only the higher-level Error class matches. The following code illustrates this point:<br />

Last updated 6/6/2012<br />

58

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

Saved successfully!

Ooh no, something went wrong!