Ensuring Success by Setting Up for Failure

Jul
6

Posted by Anthony in Flash, Tutorials tagged , , , ,

One of the great things about Adobe Flash and ActionScript 3.0 that I really like is the range of listeners available.  Specifically, I’ve been finding myself using the IOErrorEvent a lot recently, because I do a lot of work with external files, loading XML, images, and talking to databases.  Every so often, due to lack of Internet connection or an incorrectly typed path, I’ll find an error where my Flash application breaks.  While I obviously use the Event.COMPLETE listener for send and load operations, I wasn’t always using the a listener for send and load failures.  That’s where the IOErrorEvent comes in handy.

The IOErrorEvent object kicks in when there is an error sending of loading.  There is only one type of error event that fires for this, and that is IO_ERROR.  If you attach the IOErrorEvent.IO_ERROR to your send/load operation, then you can customize an error message and/or action for your user to see if your application encounters the error.  Here’s a quick example:

var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, loadXML);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, showXMLError);
myLoader.load(new URLRequest("sample.xml"));

function loadXML(e:Event):void {
   // Insert load actions
}

function showXMLError(e:IOErrorEvent):void {
   // Insert error actions
}

In this code, you’ll notice the URLLoader that loads a file named “sample.xml”.  There is a listener set up for when the XML file is loaded, and one for if an error is produced.  Then, the two functions are set up to contain the actions for each of the two outcomes.

For error functions you can do many things to alert users.  For instance, you can create and populate a text field with an error message.  Or maybe show a custom error icon.  You can even include a link or button to a contact form where they can send you (or tech support) a message about the error.  Or a button to restart the application or try to perform the failed action again.  The possibilities are endless.

So next time you’re programming a Flash application, consider using the IOErrorEvent to catch your errors and give your users meaningful error messages.  After all, there’s nothing worse than losing the interest or trust of your clients and users than to have an application that stops working with no hope of a fix or any sort of helpful error message.

Until next time, happy coding!

There are no comments yet, add one below.

Leave a Comment