【问题标题】:In flash AS3 How to put single try catch code, in order to catch any errors in whole class?在 Flash AS3 中,如何放置单次尝试捕获代码,以捕获整个班级的任何错误?
【发布时间】:2010-08-11 05:50:12
【问题描述】:

在 Flash AS3 中,我想编写单个 try catch 块来捕获整个班级中的任何错误。
比如我在myClass.as中有很多函数。我不想在每个函数中编写 try catch 块以捕获此函数中的错误。
有什么方法可以做到吗?

谢谢!

【问题讨论】:

    标签: actionscript-3 flash try-catch throw


    【解决方案1】:

    您可以使用UncaughtErrorEvent 类捕获程序中所有未处理的错误。

    所以,也许这就是你想要的。文档中有一个示例(您可以将 addEventListener 放在主类的构造函数中):

    package
    {
    import flash.display.Sprite;
    import flash.events.ErrorEvent;
    import flash.events.MouseEvent;
    import flash.events.UncaughtErrorEvent;
    
    public class UncaughtErrorEventExample extends Sprite
    {
        public function UncaughtErrorEventExample()
        {
            loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
    
            drawUI();
        }
    
        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
            }
        }
    
        private function drawUI():void
        {
            var btn:Sprite = new Sprite();
            btn.graphics.clear();
            btn.graphics.beginFill(0xFFCC00);
            btn.graphics.drawRect(0, 0, 100, 50);
            btn.graphics.endFill();
            addChild(btn);
            btn.addEventListener(MouseEvent.CLICK, clickHandler);
        }
    
        private function clickHandler(event:MouseEvent):void
        {
            throw new Error("Gak!");
        }
    }
    

    }

    请查看docs

    【讨论】:

      【解决方案2】:

      你不能!除了 AS3 之外,即使在其他语言中也没有这样简单的方法,除非他们使用 AOP 方法来做到这一点。

      最好的做法是让你的类冒泡错误(异常),让更高层捕获并处理错误。

      编辑 - 关于评论

      其实这个想法是自然的方式.. 你仍然需要手动捕捉每一个可能的错误。我给你举个例子。请注意,示例的目的只是为了清楚地区分低层和高层。

      例如,您在中间层(您的业务流程)中有一个类:

      public class MyBussiness {
          public function loadImages(){
              //for example here is a block of method
              //possibly throws exception. 
          }
      
          public function getLoan(){
              //lets assume here too
          }
      }
      

      在更高层(我假设在您的视图中 - MXML)您会捕获如下异常:

      var myBussiness:MyBussiness = new MyBussiness():
      try {
          myBussiness.loadImages();
          //any other sequence process
          myBussiness.getLoan();
      }
      catch(error:Error){
          //here you process error
          //show it to user or make LOG
      }
      

      它仍然无法像您期望的那样发挥作用,但这是最佳实践。 记住只在有可能抛出错误的代码上使用 try catch ,因为 try catch 的开销很大。

      【讨论】:

      • 还是让高层也无视吧?如果不在调试模式下,Flash 不会显示错误,对吧?
      • ktutnik,如果你能告诉我小前任这样做,那就太好了。如何通过更高层的类(包括 myClass.as)捕获错误?!
      • 谢谢,ktutnik。这是我将实现捕获错误问题的方式。祝你好运
      【解决方案3】:

      最好的方法是使用try catch:

      try{
        //your command
      } catch(e:Error){
        //your command
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多