Solved

Catching an IO Error Event in Actionscript 3

Posted on 2011-03-14
9
1,518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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
 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

726 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