JavaScript 重新抛出异常并保留堆栈跟踪
在 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;
以便使用相同的堆栈跟踪重新引发异常?
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;
}
}
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;
}
Is there a way to change the throw e;
so that the exception is rethrown with the same stack trace?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是Chrome 中的一个已知错误,遗憾的是没有解决方法据我所知。
This is a known bug in Chrome, and unfortunately there's no workaround that I'm aware of.
您能做的最好的事情就是获取原始堆栈并打印它。
我在单元测试工具中使用它。
The best you can do is grab the original stack and print it.
I use this in unit testing tools.