2013-04-18 100 views
3

使用asc2编译AIR应用程序后,发现捕获UncaughtErrorEvent时错误堆栈跟踪丢失。ActionScript编译器UncaughtErrorEvent

下面是示例代码:

var root:Sprite = this; 
root.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,errorHandle); 
throw new Error("test"); 

protected function errorHandle(event:UncaughtErrorEvent):void 
     { 
      var message:String; 
      if (event.error is Error) { 
       message = Error(event.error).message; 
       message+="\n"+Error(event.error).getStackTrace(); 
      } else if (event.error is ErrorEvent) { 
       message = ErrorEvent(event.error).text; 
      } else { 
       message = event.error.toString(); 
      } 
     } 

在使用ASC1,我可以看到在错误处理整个堆栈跟踪。但是使用ASC2,只是一个空的堆栈跟踪。

任何人都有同样的问题?

如何获得UncaughtErrorEvent堆栈跟踪?

+0

用ASC2以外的东西编译时工作正常吗? – 2013-04-18 02:14:15

+0

首先,这个问题放在一起非常糟糕,我不能编辑它使其可读。很难理解属于对方的东西,我完全不理解这个问题。其次,你会收到一条错误消息。您不会向我们提供代码,也不会告诉我们代码中发生错误的行。如果在任何try..catch块之外抛出错误,或者发送ErrorEvent对象时没有注册侦听器,则会发生未捕获的错误。你只是说“我的应用程序有错误,请帮助我。”我们该怎么做? – Joetjah 2013-04-18 07:30:45

+0

我很抱歉,我已经重新编辑它。它是否描述清楚? – hooh 2013-04-22 02:20:08

回答

7

不要投event.errorError,如:

Error(event.error).getStackTrace() 

得到一个错误的堆栈跟踪误差的建设作为一个字符串时返回错误的调用堆栈。请注意,堆栈跟踪的行号是转换的行。

相反,调用getStackTrace()event.error,如:

event.error.getStackTrace() 

没有什么根据您的例子在栈上显著。

添加递归有助于说明问题:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.UncaughtErrorEvent; 

    public class ExceptionTest extends Sprite 
    { 
     public function ExceptionTest() 
     { 
      super(); 

      loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler); 
      recursion(); 
     } 

     protected function recursion(depth:uint=0):void 
     { 
      if (depth == 5) 
       throw new Error("test"); 
      else 
       recursion(++depth); 
     } 

     protected function uncaughtErrorHandler(event:UncaughtErrorEvent):void 
     { 
      trace(event.error.getStackTrace()); 
     } 
    } 
} 

...会产生:

Error: test 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:19] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:13] 
[SWF] Users:jsturges:dev:flash-workspace:X:bin-debug:ExceptionTest.swf - 1,745 bytes after decompression 
Error: test 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:19] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:13] 

如果uncaughtErrorHandler()改为你的榜样铸造Error,如:

protected function uncaughtErrorHandler(event:UncaughtErrorEvent):void 
{ 
    trace(Error(event.error).getStackTrace()); 
} 

调试器捕获异常,但是getStackTrace是投行:

Error: test 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:19] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21] 
    at ExceptionTest()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:13] 
[SWF] Users:jsturges:dev:flash-workspace:X:bin-debug:ExceptionTest.swf - 1,519 bytes after decompression 
Error: Error: test 
    at ExceptionTest/uncaughtErrorHandler()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:26] 

也可能有细微差别编组AS1和AS2到支持UncaughtErrorEvent起始于Flash Player 10.1的AS3。

除此之外,请确保每个ActionScript版本都有适当的调试播放器。在运行时的非调试版本中,Error.getStackTrace()方法返回null