Problem
What happens if a finally block throws an exception?
What happens if the exception is thrown in the middle of a finally block? Do the remaining statements in this block (after) get executed?
Exceptions will propagate upwards, as I am aware.
Asked by Jack Kada
Solution #1
That exception will (may) propagate up the stack and be handled at a higher level.
Your finally block will not be finished once the exception has been thrown.
If the finally block was executed while an earlier exception was being handled, the original exception is lost.
Answered by Henk Holterman
Solution #2
I normally start with an empty console application project in Visual Studio and develop a tiny sample program to answer questions like these:
using System;
class Program
{
static void Main(string[] args)
{
try
{
try
{
throw new Exception("exception thrown from try block");
}
catch (Exception ex)
{
Console.WriteLine("Inner catch block handling {0}.", ex.Message);
throw;
}
finally
{
Console.WriteLine("Inner finally block");
throw new Exception("exception thrown from finally block");
Console.WriteLine("This line is never reached");
}
}
catch (Exception ex)
{
Console.WriteLine("Outer catch block handling {0}.", ex.Message);
}
finally
{
Console.WriteLine("Outer finally block");
}
}
}
When you run the program, you’ll notice that the catch and finally blocks are executed in the same order. Please notice that code in the finally block after the exception is thrown will not be executed (in fact, Visual Studio will notify you that inaccessible code has been found in this sample program):
Inner catch block handling exception thrown from try block.
Inner finally block
Outer catch block handling exception thrown from finally block.
Outer finally block
Additional Remark
If you don’t handle an exception from the try block in a (inner) catch block, it will be “devoured,” as Michael Damatov pointed out. The re-thrown exception does not appear in the outer catch block in the example above. Take a look at the somewhat modified example below to see what I mean:
using System;
class Program
{
static void Main(string[] args)
{
try
{
try
{
throw new Exception("exception thrown from try block");
}
finally
{
Console.WriteLine("Inner finally block");
throw new Exception("exception thrown from finally block");
Console.WriteLine("This line is never reached");
}
}
catch (Exception ex)
{
Console.WriteLine("Outer catch block handling {0}.", ex.Message);
}
finally
{
Console.WriteLine("Outer finally block");
}
}
}
The inner exception is “lost” (i.e. ignored) as you can see from the output:
Inner finally block
Outer catch block handling exception thrown from finally block.
Outer finally block
Answered by Dirk Vollmar
Solution #3
If an exception is currently pending (for example, when the try block has a finally but no catch), the new exception takes its place.
If no exception is pending, it behaves in the same way as throwing an exception outside the finally block.
Answered by Guffa
Solution #4
If you want to save “original exception” (thrown in try block) and sacrifice “finally exception” (thrown in finally block), here’s a quick (and quite clear) snippet:
try
{
throw new Exception("Original Exception");
}
finally
{
try
{
throw new Exception("Finally Exception");
}
catch
{ }
}
“Original Exception” propagates up the call stack when the code above is run, and “Finally Exception” gets lost.
Answered by lxa
Solution #5
The exception has been passed down.
Answered by Darin Dimitrov
Post is based on https://stackoverflow.com/questions/2911215/what-happens-if-a-finally-block-throws-an-exception