加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

actionscript-3 – ActionScript编译器UncaughtErrorEvent

发布时间:2020-12-15 07:22:06 所属栏目:百科 来源:网络整理
导读:使用asc2编译AIR应用程序后,我发现在捕获UncaughtErrorEvent时缺少错误堆栈跟踪. 这是示例代码: var root:Sprite = this;root.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,errorHandle);throw new Error("test");p
使用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堆栈跟踪?

解决方法

不要将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]

AS1和AS2也可能存在细微差别,AS3支持从Flash Player 10.1开始支持UncaughtErrorEvent.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读