5

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?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
configurator
  • 40,828
  • 14
  • 81
  • 115
  • 3
    Possible duplicate of [How can I rethrow an exception in Javascript, but preserve the stack?](http://stackoverflow.com/questions/3734236/how-can-i-rethrow-an-exception-in-javascript-but-preserve-the-stack) – cmroanirgo Jan 02 '17 at 03:00

2 Answers2

2

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

LukeH
  • 263,068
  • 57
  • 365
  • 409
1

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);
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Philip Taylor
  • 521
  • 5
  • 11
  • 1
    Note: According to this thread(https://bugs.chromium.org/p/chromium/issues/detail?id=60240) rethrowing works in Chrome now, but I find it only works in code defined in a single file. My rethrow is in a different file, and its not working. – Philip Taylor Apr 12 '16 at 14:01