How can I rethrow an exception in Javascript, but preserve the stack?

66,602

Solution 1

This is a bug in Chrome. Rethrowing an exception should preserve the call trace.

http://code.google.com/p/chromium/issues/detail?id=60240

I don't know of any workaround.

I don't see the problem with finally. I do see exceptions silently not showing up on the error console in some cases after a finally, but that one seems to be fixed in development builds.

Solution 2

The stack property of an Error object is created at the same time as the Error object itself, not at the point it's thrown. They're often the same because of the idiom

   throw new Error("message");

and if you use the code just as you've written it, the stack property will not be changed when you rethrow the error.

Solution 3

As mentioned, the stack is a snapshot created while running new Error(...), so you can't really throw the error with the same stack.

A workaround I used is to console.error the stack before throwing:

  console.error(err.stack);
  throw err;

It's not perfect, but it gets you enough debug-able information, and the stack of the original error.

Share:
66,602

Related videos on Youtube

Geoff
Author by

Geoff

Meteor core developer. (Mods, if you need to confirm this, please see http://meteor.com/about/people)

Updated on October 07, 2021

Comments

  • Geoff
    Geoff over 2 years

    In Javascript, suppose I want to perform some cleanup when an exception happens, but let the exception continue to propagate up the stack, eg:

    try {
      enterAwesomeMode();
      doRiskyStuff(); // might throw an exception
    } catch (e) {
      leaveAwesomeMode();
      throw e;
    }
    doMoreStuff();
    leaveAwesomeMode();
    

    The problem with this code is that catching and rethrowing the exception causes the stack trace information up to that point to be lost, so that if the exception is subsequently caught again, higher up on the stack, the stack trace only goes down to the re-throw. This sucks because it means it doesn't contain the function that actually threw the exception.

    As it turns out, try..finally has the same behavior, in at least Chrome (that is, it is not the re-throw that is the problem precisely, but the presence of any exception handler block at all.)

    Does anyone know of a way to rethrow an exception in Javascript but preserve the stack trace associated with it? Failing that, how about suggestions for other ways to add exception-safe cleanup handlers, while also capturing complete stack traces when an exception happens?

    Thanks for any pointers :)

  • Ted Bigham
    Ted Bigham over 6 years
    This is not true (maybe platform dependent). The js engine i'm using now (Rhino) resets the stack on the throw statement, losing the original stack.
  • Mike Stay
    Mike Stay over 6 years
    Perhaps so, but rhino-1.7.7.2.jar does not change it. What version are you using?
  • Zachary Burns
    Zachary Burns over 5 years
    This issue has since been closed.
  • Gillespie
    Gillespie over 2 years
    Still broken for me as of Chrome 92. error.stack reflects line number of rethrow not original