How To Handle Input/Output (IO) Error Caused By URLLoader in Flex 2 or ActionScript 3

Introduction:

I sometimes use class URLLoader (see reference 1) to load into my Flex applications an XML file that sets up the initial configuration of the application or provides some initial data needed by the application. For an example of using class URLLoader see reference 2.

I recently had an issue where the XML file that URLLoader was trying to load was not present (it had been deleted by mistake), so when that application ran URLLoader could not load the file. The user just saw the confusing Adobe Flash Player 9 error box since I wasn't handling the error that is generated when the URLLoader cannot load the file. Now that I've learned how to handle this error, I want to share the code in my blog. Reference 3 is an example that will show the user an error message if the URLLoader object cannot load the file. You can right click on the application to view the source code.

References:

  1. Class URLLoader, Flex 2 Language Reference
  2. Example of Using Class URLLoader, Bruce Phillips' Blog
  3. Example of Handling IO Error Caused By URLLoader
  4. Class IOErrorEvent, Flex 2 Language Reference

Discussion:

One of the events class URLLoader can broadcast is ioError, which is dispatched "if a call to URLLoader.load() results in a fatal error that terminates the download." An ioError event is of type IOErrorEvent (reference 4). You can register an event listener to listen for this event and call a specific function in response by using the addEventListener method. For example:


//load the XML file that has the feed data
var request:URLRequest = new URLRequest('feed.xml');
var loader:URLLoader = new URLLoader();
        
/*call function completeListener when the
file is completely loaded*/


loader.addEventListener(Event.COMPLETE, completeListener);
            
/*call function ioErrorListener if the
file cannot be loaded*/

loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
            
loader.load(request);

In the above code, I specify that function ioErrorListener is to be executed if the URLLoader object (loader) dispatches the IO_ERROR event. In my example (reference 3) function ioErrorListener is:


private function ioErrorListener(event:IOErrorEvent):void {
            
            
Alert.show("Failure! Unable to load the configuration
file." +
" Contact Bruce Phillips, [email protected] " +
" to report this problem.\n" + event.text );
            
            
}

Note that the parameter for the above function is of type IOErrorEvent. See reference 4 for the methods and properties that you may use with an object of type IOErrorEvent. The text property provides a brief error message that includes the name of the file that the URLLoader was trying to load.

When using class URLLoader it is a good practice to register a listener for the ioError event so that you can provide the user a more understandable error message should URLLoader be unable to load the file.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Thanks for this tip, very new to Flex and Action script.
# Posted By Thomas | 7/11/08 7:02 AM
Just wanted to say thanks. It answered my problem.
# Posted By Aaron Hardy | 7/21/08 3:01 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner