Why isn’t the line “Console.WriteLine(“asdf”);” executed? All the others are. Shouldn’t it also be as we can’t jump out from the finally scope?
static bool Func()
{
try
{
try
{
}
finally
{
try
{
throw new ApplicationException();
}
finally
{
Console.WriteLine("asd");
}
Console.WriteLine("asdf");
}
}
finally
{
Console.WriteLine("asd");
}
}
Finally blocks only guarantee (at least mostly guarantee, see excerpt from MSDN below) that they will be entered in the event that the try block throws an exception. If you throw an exception within the finally block, the exception will cause control to leave the finally block and the rest of the code within that finally block will not execute.
In your case, the line that isn’t being executed is occurring after an exception in the same finally block, so it gets skipped.
From MSDN – try-finally:
Note: Unhandled Exception Processing in the CLR is a reference to an article in the September 2008 issue of the MSDN Magazine. All 2008 and older issues of MSDN Magazine are only available as .chm files, and will need to be downloaded before viewing.