是否有任何解决方法来重新抛出异常并在Javascript中保留堆栈跟踪?

rca*_*ron 7 javascript exception-handling google-chrome

我知道Chrome 在Javascript中重新抛出异常时有一个已知错误,不会保留堆栈跟踪.

我在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 :(
}
Run Code Online (Sandbox Code Playgroud)

有没有办法保持堆栈跟踪?一个jQuery插件可能吗?任何解决方法或想法?

小智 5

在最后的catch块尝试

    throw e.stack;
Run Code Online (Sandbox Code Playgroud)

我指的是最后一个(进入浏览器的那个).如果您将try/catch嵌套得更深,则需要更改先前的抛出.

    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;
    }
Run Code Online (Sandbox Code Playgroud)

多么奇怪的错误.