加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex(try-catch-finally)机制

发布时间:2020-12-15 01:32:33 所属栏目:百科 来源:网络整理
导读:http://www.adobe.us/livedocs/flex/3_cn/langref/statements.html?? Flex try catch API 最近在使用中发现了flex异常捕捉的一个很奇特的地方,进行了多次试验,在此做个小小的总结: 测试代码如下: Mxml代码 mx:Application xmlns:mx="http://www.adobe.co

http://www.adobe.us/livedocs/flex/3_cn/langref/statements.html?? <Flex try catch API>最近在使用中发现了flex异常捕捉的一个很奇特的地方,进行了多次试验,在此做个小小的总结:

测试代码如下:

Mxml代码

  • <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"?? ??
  • ???????????????? layout="absolute" creationComplete="init()">??? ??
  • ???? <mx:Script>??? ??
  • ???????? <![CDATA[??? ??
  • ???????????? import mx.controls.Alert;??? ??
  • ???????????? private function init():void{??? ??
  • ???????????????? var a:String = null;??? ??
  • ???????????????? try{??? ??
  • ???????????????????? var myurl:URLLoader = new URLLoader();??? ??
  • ???????????????????? myurl.load(new URLRequest("aaa.xml"));??? ??
  • ???????????????? }catch(e:Error){??? ??
  • ???????????????????? Alert.show("进入捕捉1");??? ??
  • ???????????????? }??? ??
  • ???????????? }??? ??
  • ???????? ]]>??? ??
  • ???? </mx:Script>??? ??
  • </mx:Application>????
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
    layout="absolute" creationComplete="init()">   
    <mx:Script>   
    <![CDATA[   
    import mx.controls.Alert;   
    private function init():void{   
    var a:String = null;   
    try{   
    var myurl:URLLoader = new URLLoader();   
    myurl.load(new URLRequest("aaa.xml"));   
    }catch(e:Error){   
    Alert.show("进入捕捉1");   
    }   
    }   
    ]]>   
    </mx:Script>   
    </mx:Application>

    这里的xml文件"aaa.xml"是不存在的,如果按照java异常捕捉的习惯这里应该是会被捕捉的。可是在flex是用情况中,结果却是依然弹出错误提示“Error #2044: 未处理的 ioError:。 text=Error #2032: 流错误”。

    ?? 经在网上求证,可信的说法为flex的异常不是都能用try...catch捕捉的,像上面的情况必须使用监听器来使用,确实让人郁闷。

    下面就来加个监听器捕捉下这个异常,测试代码如下:

    Mxml代码
  • <?xml version="1.0" encoding="utf-8"?> ??
  • <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"??
  • ???????????????? layout="absolute" creationComplete="init()"> ??
  • ???? <mx:Script> ??
  • ???????? <![CDATA[ ??
  • ???????????? import mx.controls.Alert; ??
  • ???????????? private function init():void{ ??
  • ???????????????? var a:String = null; ??
  • ???????????????? try{ ??
  • ???????????????????? var myurl:URLLoader = new URLLoader(); ??
  • ???????????????????? myurl.load(new URLRequest("aaa.xml")); ??
  • ???????????????????? myurl.addEventListener(IOErrorEvent.IO_ERROR,onErr); ??
  • ???????????????? }catch(e:Error){ ??
  • ???????????????????? Alert.show("进入捕捉1"); ??
  • ???????????????? } ??
  • ???????????? } ??
  • ???????????? ??
  • ???????????? private function onErr(event:IOErrorEvent){ ??
  • ???????????????? Alert.show("进入监听异常捕捉!"); ??
  • ???????????? } ??
  • ???????? ]]> ??
  • ???? </mx:Script> ??
  • </mx:Application>??
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" creationComplete="init()">
    <mx:Script>
    <![CDATA[
    import mx.controls.Alert;
    private function init():void{
    var a:String = null;
    try{
    var myurl:URLLoader = new URLLoader();
    myurl.load(new URLRequest("aaa.xml"));
    myurl.addEventListener(IOErrorEvent.IO_ERROR,onErr);
    }catch(e:Error){
    Alert.show("进入捕捉1");
    }
    }
    private function onErr(event:IOErrorEvent){
    Alert.show("进入监听异常捕捉!");
    }
    ]]>
    </mx:Script>
    </mx:Application>

    ??? 运行这个测试代码,果真可以捕捉。看来flex的异常需要在逻辑上分成两种分别对待捕捉??呵呵。

    ?? 注:在测试的过程中我还发现了一个很特别的现象,现将测试结果记录在此:

    ???????? 在只是用try...catch捕捉上面的那种异常情况下。

    ?????? 测试环境为:window xp,flash 9,Adobe Flex Builder 3(以下简称fb3),tomcat5.5

    ??????? 1.在fb3中直接运行bin-debug目录下的swf文件,异常是捕捉不到的。

    ????????? 2.bin-debug目录下的swf文件不在非fb3中直接点击运行异常是可以捕捉到的。

    ???????? 3.在fb3中直接运行bin-release目录下生成的swf,异常是可以捕捉的。

    ???????? 4.bin-release目录下生成的swf在非fb3中直接点击运行异常是可以捕捉到的。

    ???????? 5.两个目录下的swf文件放在web服务器下通过ie访问,异常都不可以捕捉的。

    ???????? 6.两个目录下的swf文件放在web服务器下通过firefox访问,异常是可以捕捉的。

    ???????? 看来flex对运行平台的依赖还会很强的,还是用标准的监听去捕捉异常吧

  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读