Subscribe

  • Subscribe  

Run time error handling in AS3

Posted by Jay | May 8, 2008 .

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);

   }

}

Leave a Comment

If you would like to make a comment, please fill out the form below.


Name

Email

Website

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comments

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Related Posts

4 Comments so far
  1. CJ Cenizal  January 29, 2009 2:15 pm

    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. :)

  2. Jesse  March 27, 2009 1:57 am

    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.

  3. Oxidizer  August 23, 2011 3:56 am

    @CJ Cenzial
    I actually find it interesting o_o

  4. burak özdelice  September 22, 2011 7:33 am

    thanks for your code. But, when I use it, Flash is stop instead not keep working. I mean this is not like a try catch. We just take an event. I need to keep running flash for example If I can’t find a file on the server.

<

Direct TV Offers - usdirect has the best directtv deals