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

前端 未结 2 1155
轮回少年
轮回少年 2021-01-31 06:46

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 {
  enterAweso         


        
相关标签:
2条回答
  • 2021-01-31 07:10

    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.

    0 讨论(0)
  • 2021-01-31 07:11

    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.

    0 讨论(0)
提交回复
热议问题