I wrote a simple program to test a theory that “finally” block will always execute no matter what.
But, what i’m seeing from the below pgm is that control never seems to enter the outer finaly block.
I tried doing F5 and also Ctrl-F5 in Visual studio and it’s the same result.
Can someone explain why I’m seeing this behavior?
Output on the console window is :
inner catch
inner finally
outer catch
unhandled exeption:
..and then the app crashes
public class Program
{
static void Main()
{
try
{
try
{
string s = null;
s.ToString();
}
catch
{
Console.WriteLine("inner catch");
throw;
}
finally
{
Console.WriteLine("inner finally");
}
return;
}
catch
{
Console.WriteLine("outer catch");
throw;
}
finally
{
Console.WriteLine("outer finally");
}
}
}
The "outer finally" is run, just after the exception is handled.
Here is the output from running this code outside of the debugger:
When the exception occurred the following was displayed:

After I clicked "Cancel", the application resumed and displayed the "outer finally".
If for some reason you close down your application before it has chance to enter the "outer finally" then you will never see you message. If an application’s process is "terminated" then the finally’s are skipped in case they contain code that prevents the application from closing. Otherwise it may be impossible to terminate that process.