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

Gais picture Gais · May 7, 2009 · Viewed 11.1k times · Source

Answer

mmx picture mmx · May 7, 2009

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!).