是否有任何解决方法来重新启动异常并保留JavaScript中的堆栈跟踪? [英] Is there any workaround to rethrow an exception and preserve the stacktrace in Javascript?

查看:128
本文介绍了是否有任何解决方法来重新启动异常并保留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;
    }

有什么奇怪的错误。

这篇关于是否有任何解决方法来重新启动异常并保留JavaScript中的堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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