What happens if a finally block throws an exception?

Jack Kada picture Jack Kada · May 26, 2010 · Viewed 77.1k times · Source

If a finally block throws an exception, what exactly happens?

Specifically, what happens if the exception is thrown midway through a finally block. Do the rest of statements (after) in this block get invoked?

I am aware that exceptions will propagate upwards.

Answer

Henk Holterman picture Henk Holterman · May 26, 2010

If a finally block throws an exception what exactly happens ?

That exception propagates out and up, and will (can) be handled at a higher level.

Your finally block will not be completed beyond the point where the exception is thrown.

If the finally block was executing during the handling of an earlier exception then that first exception is lost.

C# 4 Language Specification § 8.9.5: If the finally block throws another exception, processing of the current exception is terminated.