5

在 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;以便用相同的堆栈跟踪重新抛出异常?

4

2 回答 2

2

这是Chrome 中的一个已知错误,不幸的是,我知道没有解决方法。

于 2011-03-09T17:52:35.140 回答
1

您能做的最好的事情就是抓住原始堆栈并打印它。我在单元测试工具中使用它。

try{
  ...
}
catch(e){
    console.log(e.stack);
    console.log(e.message);
    throw(e);
}
于 2016-04-12T13:50:01.687 回答