JavaScript重新抛出异常,保留堆栈跟踪 [英] JavaScript rethrowing an Exception preserving the stack trace

查看:81
本文介绍了JavaScript重新抛出异常,保留堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome中,当发生异常时,它会将堆栈跟踪打印到控制台日志。这非常有用,但不幸的是,在重新引发异常的情况下,这会导致问题。

In Chrome, when an exception occurs, it prints a stack trace to the console log. This is extremely useful, but unfortunately in cases where an exception has been rethrown this causes an issue.

} catch (e) {
    if (foo(e)) {
        // handle the exception
    } else {
        // The stack traces points here
        throw e;
    }
}

不幸的是, jQuery.js 如果它们来自事件处理程序内部,则会导致所有异常出现此问题。

Unfortunately, the following code in jQuery.js is causing all exceptions to have this issue if they're from inside event handlers.

try {
    while( callbacks[ 0 ] ) {
        callbacks.shift().apply( context, args );
    }
}
// We have to add a catch block for
// IE prior to 8 or else the finally
// block will never get executed
catch (e) {
    throw e;
}
finally {
    fired = [ context, args ];
    firing = 0;
}

有没有办法改变 throw e; 以便使用相同的堆栈跟踪重新抛出异常?

Is there a way to change the throw e; so that the exception is rethrown with the same stack trace?

推荐答案

这是一个 Chrome中的已知错误,遗憾的是我没有解决方法。

This is a known bug in Chrome, and unfortunately there's no workaround that I'm aware of.

这篇关于JavaScript重新抛出异常,保留堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆