

As most of us Flex or Flash developers are quite aware that the code we generate is often error prone; error handling is an issue that mandates serious consideration. While talking about errors we need to deal with compile time errors which are primarily due to syntax incorrectness along with run time errors that are primarily semantic errors. ActionScript 3 has built in “Error” and “ErrorEvent” classes to handle both types of exceptions.
Now let us take a closer look at how run time errors are negotiated in AS3. We find that they can of two types
Synchronous errors
When developer can foresee the possibility of errors at a place e.g. file upload or method invocation with invalid arguments. Such error can be resolved at run time using Try- catch – finally block. In this case, special error objects are being created which are subclasses of “Error” class in ActionScript 3 .
Asynchronous errors
When developer is unaware when error will occur.e.g loading data from external link. Flash Player throws an exception; run-time error that creates event and event listeners is the resolution. “ErrorEvent” class builds listener function for specific error event. Event listener (listener function) is analogous to catch block of synchronous errors. AddEventListener() method of application object maps the error event with event listener (listener function) as application is not aware time of error occurrence.
import flash.errors.IOError; import flash.events.IOErrorEvent; import flash.media.Sound; import flash.net.URLRequest; import flash.display.DisplayObject; import flash.events.*;public class Error_Event extends DisplayObject { private var mp3_play: Sound = new Sound (); // error handling try catch error event listener private function playMP3(mp3_sing:String):void { try{ mp3_play.load(new URLRequest(mp3_sing)); mp3_play.play();} catch (err1:Error) { trace(err1.message); // generates error message in error object } //asynchronous error handling mp3_play.addEventListener(IOErrorEvent.IO_ERROR, error_Handler); } private function error_Handler(error_Event:IOErrorEvent):void { trace(error_Event.text); } }
If you would like to make a comment, please fill out the form below.
Great explanation! Thank you for writing about this… it’s kind of mundane stuff, but this is the boring stuff developers need to know to get better.
Hey, thanks for this. I know what it’s like to put out work and have 10000 bastards come by, take your advice and not even leave a thanks. So, thank you.