Solved

Catching an IO Error Event in Actionscript 3

Posted on 2011-03-14
9
1,466 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:richo22
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35126924
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
0
 

Author Comment

by:richo22
ID: 35126990
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.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35127299
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
0
 

Author Comment

by:richo22
ID: 35144912
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
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 17

Expert Comment

by:deepanjandas
ID: 35144931
Great to know that the suggestions made your code perform better.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:richo22
ID: 35174137
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?
0
 

Author Comment

by:richo22
ID: 35174188
I've just discovered the answer! Code should read:

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

rather than

swfLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 500 total points
ID: 35174410
Ah! you are right, I missed that while copying your code.

Warm Regards
Deepanjan Das
0
 

Author Closing Comment

by:richo22
ID: 35174781
Thanks for your help on this one. I'll push on.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now