Search

 

October 2010
S M T W T F S
« Sep   Nov »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Tags

Archives


« | Main | »

Trapping flash.display.Loader IOErrors

By Rich Tretola | October 15, 2010
8,328 views

I was trying to debug an issue yesterday and stumbled across something in the Flex SDK that I was unaware of. I was using SDK 3.4, but this is also true of 3.5, 4.0, and 4.5.

My issue was that I had a Loader class which I was using to load image files. In some cases I knew that it was possible that the image didn’t actually exist but there was no easy way to know this programmatically. I was trying to catch the IOError with an event listener. A simplified example of the code is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                minWidth="955" minHeight="600" creationComplete="loadBadImage()">
    <mx:Script>
        <![CDATA[
            private var loader:Loader;
           
            private function loadBadImage():void{
               
                loader = new Loader();
                loader.addEventListener(IOErrorEvent.IO_ERROR,ioerror);
               
                var urlRequest:URLRequest = new URLRequest();
                // image does not exist
                urlRequest.url = "http://www.test.com/test.jpg";
                loader.load(urlRequest);
            }
           
            private function ioerror(event:IOErrorEvent):void{
                trace("ioerror");
            }
        ]]>
    </mx:Script>
</mx:Application>

It never worked! I tried adding listeners for all of the other IOErrorEvents and even added a try/catch to the entire method and still the debug player would show the error in the browser. Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

I was getting very frustrated. Then I Googled a bit and found this interesting bit of info. For some reason the Loader class dispatches IOErrors from its contentLoaderInfo property rather that directly. So the solution was simple, just change the event listener to listen for the IOError on the contentLoaderInfo property as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                minWidth="955" minHeight="600" creationComplete="loadBadImage()">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            private var loader:Loader;
           
            private function loadBadImage():void{
               
                loader = new Loader();
                loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioerror);
               
                var urlRequest:URLRequest = new URLRequest();
                // image does not exist
                urlRequest.url = "http://www.test.com/test.jpg";
                loader.load(urlRequest);
            }
           
            private function ioerror(event:IOErrorEvent):void{
                Alert.show("Caught It!", event.type );
            }
        ]]>
    </mx:Script>
</mx:Application>

BY THE WAY, IT DOES STATE THIS IN THE DOCS, BUT WHO READS THEM? :-)

Topics: ActionScript 3, Flex, Flex 2, Flex 3 (Moxie), Tutorials | 1 Comment »

One Response to “Trapping flash.display.Loader IOErrors”

  1. John C. Bland II Says:
    October 19th, 2010 at 11:08 am

    Actually, you need to trap SecurityErrorEvent.SECURITY_ERROR as well. That handles cross domain issues.

    Reply to this comment

Comments