If I return out of a try/finally block in C# does the code in the finally always run?

前端 未结 4 550
醉梦人生
醉梦人生 2020-12-05 06:46

It seems like it does as per some initial testing, but what I\'d like to know is if it is guaranteed to return or if in some cases it can not return? This i

4条回答
  •  独厮守ぢ
    2020-12-05 07:24

    In case of fatal exceptions that terminate application Finally block will not be called. Includes stack overflow, exceptions during JIT of methods to call, fatal exceptions insisde CLR runtime.

    As @mintech points out if application hangs inside the block it simply will not reach finally block. This includes waiting for synchronization objects, deadlocks infinite loops or even UI that does not have a way to close it.

提交回复
热议问题