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

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

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.SELECT, selectHandler);<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.OPEN, op<strong>en</strong>Handler);<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.COMPLETE, completeHandler);<br />

fileRef.browse();<br />

function selectHandler(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

trace("...select...");<br />

var request:URLRequest = new URLRequest("http://www.yourdomain.com/fileupload.cfm");<br />

request.method = URLRequestMethod.POST;<br />

ev<strong>en</strong>t.target.upload(request);<br />

}<br />

function op<strong>en</strong>Handler(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

trace("...op<strong>en</strong>...");<br />

}<br />

function completeHandler(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

trace("...complete...");<br />

}<br />

For detailed information on asynchronous error handling, see “Responding to error ev<strong>en</strong>ts and status” on page 63.<br />

Uncaught exceptions are errors thrown with no corresponding logic (like a catch statem<strong>en</strong>t) to respond to them.<br />

If your application throws an error, and no appropriate catch statem<strong>en</strong>t or ev<strong>en</strong>t handler can be found at the<br />

curr<strong>en</strong>t or higher level to handle the error, the error is considered an uncaught exception.<br />

Wh<strong>en</strong> an uncaught error happ<strong>en</strong>s, the runtime dispatches an uncaughtError ev<strong>en</strong>t. This ev<strong>en</strong>t is also known as a<br />

“global error handler.” This ev<strong>en</strong>t is dispatched by the SWF’s UncaughtErrorEv<strong>en</strong>ts object, which is available<br />

through the LoaderInfo.uncaughtErrorEv<strong>en</strong>ts property. If no list<strong>en</strong>ers are registered for the uncaughtError<br />

ev<strong>en</strong>t, the runtime ignores uncaught errors and tries to continue running, as long as the error doesn’t stop the SWF.<br />

In addition to dispatching the uncaughtError ev<strong>en</strong>t, debugger versions of the Flash runtime respond to uncaught<br />

errors by terminating the curr<strong>en</strong>t script. Th<strong>en</strong>, they display the uncaught error in trace statem<strong>en</strong>t output or<br />

writing the error message to a log file. If the exception object is an instance of the Error class or one of its subclasses,<br />

the getStackTrace() method is called. The stack trace information is also displayed in trace statem<strong>en</strong>t output or<br />

in a log file. For more information about using the debugger version of Flash runtimes, see “Working with the<br />

debugger versions of Flash runtimes” on page 57.<br />

Note: While processing an uncaughtError ev<strong>en</strong>t, if an error ev<strong>en</strong>t is thrown from an uncaughtError handler, the ev<strong>en</strong>t<br />

handler is called multiple times. This results in an infinite loop of exceptions. It is recomm<strong>en</strong>ded that you avoid such<br />

a sc<strong>en</strong>ario.<br />

Error handling in ActionScript 3.0<br />

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

Since many applications can run without building the logic to handle errors, developers are tempted to postpone<br />

building error handling into their applications. However, without error handling, an application can easily stall or<br />

frustrate the user if something doesn’t work as expected. ActionScript 2.0 has an Error class that allows you to build<br />

logic into custom functions to throw an exception with a specific message. Because error handling is critical for making<br />

a user-fri<strong>en</strong>dly application, ActionScript 3.0 includes an expanded architecture for catching errors.<br />

Last updated 6/6/2012<br />

55

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

Saved successfully!

Ooh no, something went wrong!