Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Catching an IO Error Event in Actionscript 3

Posted on 2011-03-14
9
Medium Priority
?
1,561 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
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…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
Suggested Courses

618 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