How to rethrow an exception in JavaScript, but preserve the stack?

Sometimes, we want to rethrow an exception in JavaScript, but preserve the stack.

In this article, we’ll look at how to rethrow an exception in JavaScript, but preserve the stack.

How to rethrow an exception in JavaScript, but preserve the stack?

To rethrow an exception in JavaScript, but preserve the stack, we just throw the exception.

For instance, we write

throw err;

to throw the err Error instance with throw.

Conclusion

To rethrow an exception in JavaScript, but preserve the stack, we just throw the exception.