Actionscript – How to handle Unhandled IOErrorEvent

actionscriptactionscript-3airflash

How to Handle\catch this error

Unhandled IOErrorEvent:. text=Error #2124: Loaded file is an unknown type.

I try to load a corrupted image in to MovieClip with AS3
I tried to use try & catch but no way
I alse try to addEventListener

loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

but it doesn't catch this error

Any Help?!

Best Solution

If you want to catch any unseen errors, you can use a standard try-catch block.

var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener("complete", ldrDone);
ldr.contentLoaderInfo.addEventListener("ioError", ldrError);
ldr.load(new URLRequest("FILE-NAME-COMES-HERE"));

function ldrDone(evt:*):void
{
    //if the file can be loaded into a Loader object, this part runs
    var temp:*;

    try
    {
        temp = evt.target.content;
        //add it to the stage
        stage.addChild(temp);

        //this traces whether the loaded content is a Bitmap (jpg, gif, png) or a MovieClip (swf)
        var classOfObject:String = flash.utils.getQualifiedClassName(temp);
        trace(classOfObject);
    }
    catch(error:*)
    {
        trace("some error was caught, for example swf is AS2, or whatever, like Error #2180");
    }
}

function ldrError(evt:*):void
{
    //if the file can't be loaded into a Loader object, this part runs
    trace("this is the error part, Error #2124 won't show up");
}

This catches errors, like the swf you are trying to load is an old swf (published with AS2) - Error #2180.

If the file can't be found, or doesn't look like to be of any of the loadable formats, then the ioError part runs - Error #2124.

Related Question