Solved

Catching an IO Error Event in Actionscript 3

Posted on 2011-03-14
9
1,508 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
Technology Partners: 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
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 set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

739 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