what can lead throw to reset a callstack (I'm using “throw”, not “throw ex”)

后端 未结 3 1191
别跟我提以往
别跟我提以往 2020-11-30 00:35

I\'ve always thought the difference between \"throw\" and \"throw ex\" was that throw alone wasn\'t resetting the stacktrace of the exception.

Unfortunately, that\'s

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 01:30

    The problem is that Windows is resetting the stack's starting point. The CLR is behaving as expected—this is just a limitation of the host operating system's exception handling support. The problem is that there can only be one stack frame per method call.

    You could extract your exception handling routines into a separate "helper" method, which would work around the limitations imposed by Windows's SEH, but I don't think that's necessarily a good idea.

    The proper way to rethrow an exception without losing the stack information is to throw a new exception and include the original, caught exception as the inner exception.

    It's difficult to imagine very many cases where you'd really need to do this. If you're not handling the exception, and simply catching it to rethrow it, you probably shouldn't be catching it in the first place.

提交回复
热议问题