ChatGPT解决这个技术问题 Extra ChatGPT

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

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


G
Glenn Maynard

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.


This issue has since been closed.
Still broken for me as of Chrome 92. error.stack reflects line number of rethrow not original
j
jpaugh

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.


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.
Perhaps so, but rhino-1.7.7.2.jar does not change it. What version are you using?
O
Ofer Segev

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.


t
texuf

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

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