Link to home
Start Free TrialLog in
Avatar of richo22
richo22Flag for Australia

asked on

Catching an IO Error Event in Actionscript 3

I'm writing an application is Flash CS5 using Actionscript 3.0. I'm trying to incorporate try and catch statements to catch an IO error if the loader cannot load an external SWF. (At the moment the program just stops if it encounters an error).

However the try, catch statements are not catching the error. Here's a bit of example code:

var swfLoader:Loader = new Loader();

function loadSWF():void
{
      addChild(swfLoader);
      var myUrl:URLRequest = new URLRequest(“Invalid file name.swf”);
      swfLoader.load(myUrl);
}

try
{
      loadSWF();
      now do some other stuff;
        trace(“I’ve done the other stuff”);
}catch(error:IOError)
{
      trace(“Yoo Hoo! An IO error has been caught”);
}

When I compile the program it will run through and output "I've done the other stuff" and then stop. I never get my "Yoo Hoo!" error message. The compiler will then give me its unhandled IO error message. So the catch statement is not catching the error.

I then read that this type of IO error is called an asynchronous error, and must be handled by adding an event listener to the swfLoader. Thus:


swfLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
      
function catchIOError(event:IOErrorEvent)
{
trace("Error caught: "+event.type);
}

I've tried this but it doesn't catch the error either.

Can anyone point me in the right direction?
Avatar of deepanjandas
deepanjandas
Flag of India image

Try this sequence:
var swfLoader:Loader = new Loader();
swfLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
      
function loadSWF():void{
      addChild(swfLoader);
      var myUrl:URLRequest = new URLRequest(“Invalid file name.swf”);
      swfLoader.load(myUrl);
}

function catchIOError(event:IOErrorEvent):void{
   trace("Error caught: "+event.type);
}

try{
      loadSWF();
      now do some other stuff;
        trace(“I’ve done the other stuff”);
}catch(error:Error){
      trace("Yoo Hoo! An IO error has been caught");
}

Open in new window


Warm Regards
Deepanjan Das
Avatar of richo22

ASKER

Thanks for getting back to me so quickly.

I've just tried your suggested sequence, but I don't get any "Yoo Hoo!" message. Still not getting into the catch block.
The catch block will be entered if the file to be loaded has invalid data, not when the link is a broken one.
Try with this:
var myUrl:URLRequest = new URLRequest("Invalid file name.swf");
var swfLoader:Loader = new Loader();
swfLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
addChild(swfLoader);

 
function loadSWF():void{
      try{
          swfLoader.load(myUrl);
          //now do some other stuff;
          trace("I’ve done the other stuff");
      }catch(error:Error){
          trace("Yoo Hoo! An IO error has been caught");
      }
}

function catchIOError(event:IOErrorEvent):void{
   trace("Error caught: "+event.type);
}

loadSWF();

Open in new window


Warm Regards
Deepanjan Das
Avatar of richo22

ASKER

Hi Deepanjan,

I've had to go away for a couple of days, but I will pick this up when I get back. I haven't had a chance yet to try your suggestion, but I will.

My application is designed to run on the computer hard drive, rather than from the web. So broken links aren't a problem, nor are invalid swf file names. I'm just using an invalid file name to generate an IO error while I'm trying to work out the code.

By the way, I read through your suggestions on best practice actionscript code, and have changed a number of things in my code. It has made a big difference. I just need to get the exception handling sorted out, for the very odd occasion when an external swf fails to load properly.

I'm assuming this is an IO error.

Regards,

Richard
Great to know that the suggestions made your code perform better.

Warm Regards
Deepanjan Das
Avatar of richo22

ASKER

OK. I've tried the suggested code, but I still can't catch the error.

When I compile and run the program, it stops at this point, and the compiler output says:
"Error #2044: Unhandled IOErrorEvent: text=Error #2035: URL Not Found."

Why doesn't the event listener catch this error, do you think?
Avatar of richo22

ASKER

I've just discovered the answer! Code should read:

swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);

rather than

swfLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
ASKER CERTIFIED SOLUTION
Avatar of deepanjandas
deepanjandas
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of richo22

ASKER

Thanks for your help on this one. I'll push on.