【问题标题】:ActionScript Compiler UncaughtErrorEventActionScript 编译器 UncaughtErrorEvent
【发布时间】:2013-04-18 02:06:50
【问题描述】:

使用 asc2 编译 AIR 应用程序后,我发现在捕获 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 堆栈跟踪?

【问题讨论】:

  • 在使用 ASC2 以外的其他东西进行编译时是否有效?
  • 首先,这个问题拼凑得非常糟糕,我无法对其进行编辑以使其具有可读性。很难理解什么是彼此的,我也不完全理解这个问题。其次,您会收到一条错误消息。您没有向我们提供代码,也没有告诉我们错误发生在代码的哪一行。当在任何 try..catch 块之外抛出错误或在没有注册侦听器的情况下调度 ErrorEvent 对象时,会发生未捕获的错误。你只是说“我的申请有错误,帮帮我。”我们应该怎么做?
  • 我很抱歉,我已经重新编辑了它。描述清楚了吗?

标签: actionscript-3 flash air


【解决方案1】:

不要将event.error 转换为Error,如:

Error(event.error).getStackTrace()

获取错误的堆栈跟踪以字符串形式返回错误构造时的错误调用堆栈。请注意,堆栈跟踪的行号是强制转换的行。

改为从event.error 调用getStackTrace(),如下所示:

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]

从 Flash Player 10.1 开始,将 AS1 和 AS2 编组为支持 UncaughtErrorEvent 的 AS3 也可能存在细微差别。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多