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.