在 Chrome 中,当发生异常时,它会将堆栈跟踪打印到控制台日志。这非常有用,但不幸的是,在重新抛出异常的情况下,这会导致问题。
} catch (e) {
if (foo(e)) {
// handle the exception
} else {
// The stack traces points here
throw e;
}
}
不幸的是,jQuery.js
中的以下代码会导致所有来自事件处理程序内部的异常都出现此问题。
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;
以便使用相同的堆栈跟踪重新抛出异常?
最佳答案
这是一个 known bug in Chrome ,不幸的是,据我所知没有解决方法。
关于JavaScript 重新抛出异常并保留堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5249960/