Will finally blocks be executed if returning from try or catch blocks in C#? If so, before returning or after?

前端 未结 4 1259
猫巷女王i
猫巷女王i 2020-12-15 23:25

No content available!

4条回答
  •  不思量自难忘°
    2020-12-16 00:16

    With two-pass exception handling, which .NET inherits from windows, you can't precisely say that the finally block executes before control passes back to the caller.

    The finally block will execute after finally blocks in more nested call frames, and before finally blocks and the catch block in less nested call frames, which is consistent with the finally block running before returning. But all exception filters between the throw point and the catch point will run before any finally blocks, which means that in the presence of an exception some caller code can run before the finally block.

    When control leaves the block normally (no exception thrown), then the finally runs before control returns to the caller.

提交回复
热议问题