views:

15

answers:

1

Hi, I'm creating a flash application which loads in some XML which is generated dynamically from the CMS. I want to display an error in case the XML file isn't formatted correctly. When I test this with incorrectly formatted XML, it will just get to the line myXML = XML(myLoader.data); and then just bomb out. How can I catch the error, display a message to the user, but the flash program to continue as normal.

var myXMLURL:URLRequest = new URLRequest(XMLfile); 
var myLoader:URLLoader = new URLLoader(myXMLURL); 
myLoader.addEventListener(Event.COMPLETE, xmlLoaded); 
myLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlFailed);
var myXML:XML;

//--when the xml is loaded, do this
function xmlLoaded(e:Event):void 
{ 
    myXML = XML(myLoader.data);
    trace("XML = "+myXML);
}

//--if the xml fails to load, do this
function xmlFailed(event:IOErrorEvent):void
{
    errorMsg.text = "The XML file cannot be found"  
}
+1  A: 

Just put the code that could throw an exception inside a try/catch block

private function xmlLoaded(e:Event):void 
{
     try 
     {
         myXML = XML(myLoader.data); 
         trace("XML = "+myXML); 
     }
     catch (error:Error) 
     {
         errorMsg.text = "The XML file cannot be found.";
     }
}
Jason W
Just tried that and it works brilliantly, thanks!
David Ball
np, glad to hear it :)
Jason W