是否有任何解决方法来重新启动异常并保留JavaScript中的堆栈跟踪?
问题描述:
我知道Chrome的已知错误不保存在JavaScript中重新启动异常时的堆栈跟踪。
I know that Chrome has a known bug not preserving the stacktrace when rethrowing an exception in Javascript.
我在Chrome中运行以下代码:
I have the following code running in Chrome:
try {
try {
runCodeThatMayThrowAnException();
} catch (e) {
// I'm handing the exception here (displaying a nice message or whatever)
// Now I want to rethrow the exception
throw (e);
}
} catch (e) {
// The stacktrace was lost here :(
}
有没有办法保持堆栈跟踪?jQuery插件可能是任何解决方法或想法?
Is there any way to keep the stacktrace? A jQuery plug-in maybe? Any workaround or ideas?
答
在最终的catch块中尝试
In the final catch block try
throw e.stack;
我的意思是最后一个(浏览器中的一个),如果你更深入地嵌套你的try / catch,需要更改你之前的投掷。
And I mean the very last one (the one going to the browser). If you nest your try/catch deeper, you'd need to change your previous throws.
function throwError() {
var test = _undefined.propertyAccess;
}
try {
try {
try {
throwError();
} catch (e) {
alert("exception: " + e.stack);
throw e;
}
} catch (e) {
console.log(e.stack);
throw e;
}
} catch (e) {
throw e.stack;
}
有什么奇怪的错误。