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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Handling errors<br />

Run-time errors occur wh<strong>en</strong> you run your application after you compile it. Run-time errors repres<strong>en</strong>t errors that<br />

are caused while a SWF file plays in a Flash runtime (such as Adobe Flash Player or Adobe AIR). In most cases, you<br />

handle run-time errors as they occur, reporting them to the user and taking steps to keep your application running.<br />

If the error is a fatal error, such as not being able to connect to a remote website or load required data, you can use<br />

error handling to allow your application to finish, gracefully.<br />

Synchronous errors are run-time errors that occur at the time a function is called—for example, wh<strong>en</strong> you try to use<br />

a specific method and the argum<strong>en</strong>t you pass to the method is invalid, so the Flash runtime throws an exception.<br />

Most errors occur synchronously—at the time the statem<strong>en</strong>t executes—and the flow of control passes immediately<br />

to the most applicable catch statem<strong>en</strong>t.<br />

For example, the following code excerpt throws a run-time error because the browse() method is not called before<br />

the program attempts to upload a file:<br />

var fileRef:FileRefer<strong>en</strong>ce = new FileRefer<strong>en</strong>ce();<br />

try<br />

{<br />

fileRef.upload(new URLRequest("http://www.yourdomain.com/fileupload.cfm"));<br />

}<br />

catch (error:IllegalOperationError)<br />

{<br />

trace(error);<br />

// Error #2037: Functions called in incorrect sequ<strong>en</strong>ce, or earlier<br />

// call was unsuccessful.<br />

}<br />

In this case, a run-time error is thrown synchronously because Flash Player determined that the browse() method<br />

was not called before the file upload was attempted.<br />

For detailed information on synchronous error handling, see “Handling synchronous errors in an application” on<br />

page 58.<br />

Asynchronouserrors are run-time errors that occur at various points during runtime; they g<strong>en</strong>erate ev<strong>en</strong>ts and ev<strong>en</strong>t<br />

list<strong>en</strong>ers catch them. An asynchronous operation is one in which a function initiates an operation, but doesn’t wait<br />

for it to complete. You can create an error ev<strong>en</strong>t list<strong>en</strong>er to wait for the application or user to try some operation.<br />

If the operation fails, you catch the error with an ev<strong>en</strong>t list<strong>en</strong>er and respond to the error ev<strong>en</strong>t. Th<strong>en</strong>, the ev<strong>en</strong>t<br />

list<strong>en</strong>er calls an ev<strong>en</strong>t handler function to respond to the error ev<strong>en</strong>t in a useful manner. For example, the ev<strong>en</strong>t<br />

handler could launch a dialog box that prompts the user to resolve the error.<br />

Consider the file-upload synchronous error example pres<strong>en</strong>ted earlier. If you successfully call the browse()<br />

method before beginning a file upload, Flash Player would dispatch several ev<strong>en</strong>ts. For example, wh<strong>en</strong> an upload<br />

starts, the op<strong>en</strong> ev<strong>en</strong>t is dispatched. Wh<strong>en</strong> the file upload operation completes successfully, the complete ev<strong>en</strong>t is<br />

dispatched. Because ev<strong>en</strong>t handling is asynchronous (that is, it does not occur at specific, known, predesignated<br />

times), use the addEv<strong>en</strong>tList<strong>en</strong>er() method to list<strong>en</strong> for these specific ev<strong>en</strong>ts, as the following code shows:<br />

Last updated 6/6/2012<br />

54

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

Saved successfully!

Ooh no, something went wrong!