不要投射event.error
到Error
,如:
Error(event.error).getStackTrace()
获取错误的堆栈跟踪将在错误构造为字符串时返回错误的调用堆栈。请注意,堆栈跟踪的行号是强制转换的行。
相反,调用getStackTrace()
from 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]
UncaughtErrorEvent
将 AS1 和 AS2 编组到支持从 Flash Player 10.1 开始的AS3 也可能存在细微差别。
除此之外,请确保每个 ActionScript 版本都有适当的调试播放器。在运行时的非调试器版本中,该Error.getStackTrace()
方法返回null
.