admin管理员组

文章数量:1313121

In Chrome, when an exception occurs, it prints a stack trace to the console log. This is extremely useful, but unfortunately in cases where an exception has been rethrown this causes an issue.

} catch (e) {
    if (foo(e)) {
        // handle the exception
    } else {
        // The stack traces points here
        throw e;
    }
}

Unfortunately, the following code in jQuery.js is causing all exceptions to have this issue if they're from inside event handlers.

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;
}

Is there a way to change the throw e; so that the exception is rethrown with the same stack trace?

In Chrome, when an exception occurs, it prints a stack trace to the console log. This is extremely useful, but unfortunately in cases where an exception has been rethrown this causes an issue.

} catch (e) {
    if (foo(e)) {
        // handle the exception
    } else {
        // The stack traces points here
        throw e;
    }
}

Unfortunately, the following code in jQuery.js is causing all exceptions to have this issue if they're from inside event handlers.

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;
}

Is there a way to change the throw e; so that the exception is rethrown with the same stack trace?

Share Improve this question edited Nov 5, 2013 at 23:23 Benjamin Gruenbaum 277k89 gold badges519 silver badges514 bronze badges asked Mar 9, 2011 at 17:47 configuratorconfigurator 41.7k14 gold badges84 silver badges117 bronze badges 1
  • 3 Possible duplicate of How can I rethrow an exception in Javascript, but preserve the stack? – cmroanirgo Commented Jan 2, 2017 at 3:00
Add a ment  | 

2 Answers 2

Reset to default 4

This is a known bug in Chrome, and unfortunately there's no workaround that I'm aware of.

The best you can do is grab the original stack and print it. I use this in unit testing tools.

try{
  ...
}
catch(e){
    console.log(e.stack);
    console.log(e.message);
    throw(e);
}

本文标签: JavaScript rethrowing an Exception preserving the stack traceStack Overflow