I wrote simple C# console app:
class Mystery
{
static void Main(string[] args)
{
MakeMess();
}
private static void MakeMess()
{
try
{
System.Console.WriteLine("try");
throw new Exception(); // let's invoke catch
}
catch(Exception)
{
System.Console.WriteLine("catch");
throw new Exception("A");
}
finally
{
System.Console.WriteLine("finally");
throw new Exception("B");
}
}
}
The output given in console is:
try
catch
Unhandled Exception: System.Exception: A at Mystery.Program.MakeMess() in ...
It seems that CLR caught A and the finally block was not invoked at all.
But when I surround call to MakeMess() with try-catch block:
static void Main(string[] args)
{
try
{
MakeMess();
}
catch(Exception ex)
{
System.Console.WriteLine("Main caught " + ex.Message);
}
}
The output looks totally different:
try
catch
finally
Main caught B
It seems that Exception that propagates from MakeMess() is different when Exception is strictly handled outside of method.
What's the explanation of this behavior?