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

4

215

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 :)

Mcclees answered 17/9, 2010 at 9:46 Comment(1)
Dupe: #5250460Alignment
C
98

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.

Cranston answered 13/12, 2010 at 23:10 Comment(2)
This issue has since been closed.Exert
Still broken for me as of Chrome 92. error.stack reflects line number of rethrow not originalCookson
W
36

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.

Wayzgoose answered 17/9, 2010 at 17:23 Comment(2)
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.Barnett
Perhaps so, but rhino-1.7.7.2.jar does not change it. What version are you using?Wayzgoose
B
6

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.

Blackpoll answered 6/10, 2021 at 14:50 Comment(0)
E
-3

Don't catch it in the first place, just use finally

try {
  enterAwesomeMode();
  doRiskyStuff(); // might throw an exception
  doMoreStuff();
} finally {
  leaveAwesomeMode();
}
Eskilstuna answered 2/6, 2022 at 19:12 Comment(3)
If you need to do some conditional processing in your catch-section this may not be an option, e.g., check for some condition and handle that, otherwise rethrow the same exception.Muslim
or you want to log the error but continuePromising
The question was how to rethrow the exception and preserve the callstack. If you're logging then continuing, none of this applies And Jan, yes, if you want to add some tricky logic in your catch, then this might not apply, but that was not part of the original questionEskilstuna

© 2022 - 2024 — McMap. All rights reserved.