如何在Javascript中重新启动异常,但保留堆栈? [英] How can I rethrow an exception in Javascript, but preserve the stack?

查看:167
本文介绍了如何在Javascript中重新启动异常,但保留堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,假设当异常发生时我想执行一些清理,但是让异常继续传播到堆栈,例如:

In Javascript, suppose I want to perform some cleanup when an exception happens, but let the exception continue to propagate up the stack, eg:

try {
  enterAwesomeMode();
  doRiskyStuff(); // might throw an exception
} catch (e) {
  leaveAwesomeMode();
  throw e;
}
doMoreStuff();
leaveAwesomeMode();

此代码的问题是捕获和重新抛出异常会导致堆栈跟踪信息到这一点要丢失,所以如果这个异常被再次被捕获,堆栈上的堆栈跟踪只会下降到重新抛出。这是因为它意味着它不包含实际抛出异常的功能。

The problem with this code is that catching and rethrowing the exception causes the stack trace information up to that point to be lost, so that if the exception is subsequently caught again, higher up on the stack, the stack trace only goes down to the re-throw. This sucks because it means it doesn't contain the function that actually threw the exception.

事实证明,try..finally具有相同的行为,至少Chrome(也就是说,这不是正确的问题,而是存在任何异常处理程序阻塞。)

As it turns out, try..finally has the same behavior, in at least Chrome (that is, it is not the re-throw that is the problem precisely, but the presence of any exception handler block at all.)

有谁知道一个在JavaScript中重新启动异常的方法,但保留与之相关的堆栈跟踪?如果没有这样的建议,其他方法可以添加异常安全的清理处理程序,同时在异常发生时也捕获完整的堆栈跟踪?

Does anyone know of a way to rethrow an exception in Javascript but preserve the stack trace associated with it? Failing that, how about suggestions for other ways to add exception-safe cleanup handlers, while also capturing complete stack traces when an exception happens?

感谢任何指针:)

推荐答案

这是Chrome中的一个错误。转发异常应保留通话跟踪。

This is a bug in Chrome. Rethrowing an exception should preserve the call trace.

http://code.google.com/p/chromium/issues/detail?id=60240

我不知道有什么办法。

最后我看不到问题。在某些情况下,我终于明白了在错误控制台上不会显示异常,但是似乎在开发版本中修复了异常。

I don't see the problem with finally. I do see exceptions silently not showing up on the error console in some cases after a finally, but that one seems to be fixed in development builds.

这篇关于如何在Javascript中重新启动异常,但保留堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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