Search code examples
c#.netfinally

In C# will the Finally block be executed in a try, catch, finally if an unhandled exception is thrown?


Another interview question which was expecting a true / false answer and I wasn't too sure.

Duplicate


Solution

  • finally is executed most of the time. It's almost all cases. For instance, if an async exception (like StackOverflowException, OutOfMemoryException, ThreadAbortException) is thrown on the thread, finally execution is not guaranteed. This is why constrained execution regions exist for writing highly reliable code.

    For interview purposes, I expect the answer to this question to be false (I won't guarantee anything! The interviewer might not know this herself!).