【问题标题】:How to catch all exceptions in Flex?如何捕获 Flex 中的所有异常?
【发布时间】:2010-09-11 05:17:01
【问题描述】:

当我在调试 Flash 播放器中运行 Flex 应用程序时,一旦发生意外情况,就会弹出异常。但是,当客户使用该应用程序时,他不会使用调试 Flash 播放器。在这种情况下,他没有弹出异常,但他的 UI 无法正常工作。

因此,出于可支持性的原因,我想捕获任何可能发生在 Flex UI 中的任何异常,并在 Flex 内部弹出窗口中显示错误消息。通过使用 Java,我只需将整个 UI 代码封装在一个 try/catch 块中,但是对于 Flex 中的 MXML 应用程序,我不知道在哪里可以执行这样一个通用的 try/catch。

【问题讨论】:

    标签: apache-flex exception error-handling


    【解决方案1】:

    在 Flex 3 中无法收到有关未捕获异常的通知。Adobe 已意识到该问题,但我不知道他们是否计划创建解决方法。

    目前唯一的解决方案是将 try/catch 放在合理的位置,并确保您正在侦听任何调度它们的 ERROR(或 Web 服务的 FAULT)事件。

    编辑: 此外,实际上不可能捕获事件处理程序引发的错误。我在 Adob​​e Bug System 上记录了bug

    2010-01-12 更新:Flash 10.1AIR 2.0(均处于测试阶段)现在支持全局错误处理,并通过订阅 @987654325 的 UNCAUGHT_ERROR 事件来实现@。以下代码取自code sample on livedocs

    public class UncaughtErrorEventExample extends Sprite
    {
        public function UncaughtErrorEventExample()
        {
            loaderInfo.uncaughtErrorEvents.addEventListener(
                UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
        }
    
        private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
        {
            if (event.error is Error)
            {
                var error:Error = event.error as Error;
                // do something with the error
            }
            else if (event.error is ErrorEvent)
            {
                var errorEvent:ErrorEvent = event.error as ErrorEvent;
                // do something with the error
            }
            else
            {
                // a non-Error, non-ErrorEvent type was thrown and uncaught
            }
        }
    

    【讨论】:

    • Flash 10.1 中的全局错误处理是否需要使用 flex 3.5? 4?或者它也适用于 Flex 3?
    • 我上面的代码需要 Flex 4。但是,如果您使用 ((IEventDispatcher)loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", handlerFunction),它应该可以在任何针对 10.1 运行的 SDK 中工作,因为这些属性将在运行时存在于播放器中。你甚至可以用if (loaderInfo.hasProperty("uncaughtErrorEvents") { } 包装它,以确保它不会在 Flash 9/10 中中断(当然,错误处理不起作用,但它不会崩溃)
    • @Richard 的评论:这确实符合您的预期,但不幸的是,事实并非如此。如果您使用 Flash Player 9 作为目标进行编译,并在 Flash Player 10.1 上运行它,则 loaderInfo["uncaughtErrorEvents"] 仍然不可用!我的解释:Flash 播放器在运行时会查看您的 swf 的目标播放器,并“隐藏”该版本中尚未包含的功能。
    • @Wouter - 我也看到了这种行为。随意投票/添加 cmets 到我的错误:bugs.adobe.com/jira/browse/FB-27199
    • 添加到这个答案:如果您在 Flash Player 的调试版本中运行,一般运行时错误对话框仍然会弹出。为了防止这种情况,请在全局错误处理程序中调用 event.preventDefault()。
    【解决方案2】:

    在 Adob​​e 错误管理系统中有一个错误/功能请求。如果它对您很重要,请投票给它。

    http://bugs.adobe.com/jira/browse/FP-444

    【讨论】:

      【解决方案3】:

      它适用于 Flex 3.3。

       if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
          IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler);
       }
      

      【讨论】:

        【解决方案4】:

        请注意,自 2009 年 10 月以来,错误 FP-444(上图)链接到 http://labs.adobe.com/technologies/flashplayer10/features.html#developer 表明这将在 10.1 时成为可能,目前,2009 年 10 月 28 日仍未发布 - 所以我想我们会看看是否当它被释放时就是这样

        【讨论】:

          【解决方案5】:

          使用 try-catch 替代已接受的答案。我认为速度较慢,但​​更易于阅读。

          try {
              loaderInfo.uncaughtErrorEvents.addEventListener("uncaughtError", onUncaughtError);
          } catch (e:ReferenceError) {
              var spl:Array = Capabilities.version.split(" ");
              var verSpl:Array = spl[1].split(",");
          
              if (int(verSpl[0]) >= 10 &&
                  int(verSpl[1]) >= 1) {
                  // This version is 10.1 or greater - we should have been able to listen for uncaught errors...
                  d.warn("Unable to listen for uncaught error events, despite flash version: " + Capabilities.version);
              }
          }
          

          当然,您需要使用最新的 10.1 playerglobal.swc 才能成功编译此代码: http://labs.adobe.com/downloads/flashplayer10.html

          【讨论】:

            【解决方案6】:

            我正在使用 flex 4。 我尝试了loaderInfo.UncaughtErrorEvents,,但 loaderInfo 没有初始化,所以它给了我空引用错误。然后我尝试了root.loaderInfo.UncaughtErrorEvents 和同样的故事。 我试过sprite.root.UncaughtErrorEvents,但没有精灵对象,我创建了一个,但它不起作用。最后我尝试了

            systemManager.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,globalUnCaughtErrorHandler.hanleUnCaughtError);

            你猜怎么着,它就像魔法一样。 检查this

            【讨论】:

              【解决方案7】:

              它适用于 Flex 3.5 和 flash player 10:

                <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" addedToStage="application1_addedToStageHandler(event)">
                  <mx:Script>
                      <![CDATA[
                          import mx.events.FlexEvent;
                          
                          protected function application1_addedToStageHandler(event:Event):void{              
                              if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
                                  IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler);
                              }
                              
                              sdk.text = "Flex " + mx_internal::VERSION;
                          }
                          
                          private function uncaughtErrorHandler(e:*):void{
                              e.preventDefault();
                              
                              var s:String;
              
                              if (e.error is Error)
                              {
                                  var error:Error = e.error as Error;
                                  s = "Uncaught Error: " + error.errorID + ", " + error.name + ", " + error.message;
                              }
                              else
                              {
                                  var errorEvent:ErrorEvent = e.error as ErrorEvent;
                                  s = "Uncaught ErrorEvent: " + errorEvent.text;
                              }
                              
                              msg.text = s;
                          }
                          
                          private function unCaught():void
                          {
                              var foo:String = null;
                              trace(foo.length);
                          }
                      ]]>
                  </mx:Script>
                  <mx:VBox>
                      <mx:Label id="sdk" fontSize="18"/>
                      <mx:Button y="50" label="UnCaught Error" click="unCaught();" />
                      <mx:TextArea id="msg" width="180" height="70"/>
                  </mx:VBox>
              </mx:Application>
              

              谢谢

              【讨论】:

                【解决方案8】:

                我将事件侦听器附加到“根”,这对我有用:

                sprite.root.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
                

                在调试 Flash Player 中,这仍然会出错,但在非调试版本中,错误将出现在 Flash Player 的对话框中 - 然后处理程序将做出响应。要停止出现对话框,请添加:

                event.preventDefault();
                

                所以:

                    private function onUncaughtError(event:UncaughtErrorEvent):void
                    {
                        event.preventDefault();
                        // do something with this error
                    }
                

                我在 AIR 中使用它,但我认为它也适用于标准 AS3 项目。

                【讨论】:

                  【解决方案9】:

                  现在您可以使用加载器信息:

                  http://www.adobe.com/devnet/flex/articles/global-exception-handling.html

                  结帐:

                  loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
                  
                  private function onUncaughtError(e:UncaughtErrorEvent):void
                  {
                      // Do something with your error.
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 2018-07-15
                    • 1970-01-01
                    • 1970-01-01
                    • 2023-03-16
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-04-10
                    • 2013-12-22
                    相关资源
                    最近更新 更多