Flash AS3 - Display an error if the XML if incorrect

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 11:44:06

问题


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:


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.";
     }
}


来源:https://stackoverflow.com/questions/3005664/flash-as3-display-an-error-if-the-xml-if-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!