I see that the Finally in Try .. Catch will always execute after any parts of the execution of the try catch block.
Is it any different to
Code with four radio buttons:
Finish CATCH
private void checkFinally()
{
try
{
doFinally();
}
catch
{
Console.WriteLine(" Breaking news: a crash occured. ");
}
}
private void doFinally()
{
Console.WriteLine(" ");
Console.Write("Here goes: "
+ (radioReturnInTry.Checked ? "2. Return in try: "
: (radioReturnInCatch.Checked? "3. Retrun in catch: "
: (radioThrowInCatch.Checked? "4. Throw in catch: "
: "1. Continue in catch: "))) );
try
{
if (radioReturnInTry.Checked)
{
Console.Write(" Returning in try. ");
return;
}
Console.Write(" Throwing up in try. ");
throw new Exception("check your checkbox.");
}
catch (Exception ex)
{
Console.Write(" ...caughtcha! ");
if (radioReturnInCatch.Checked)
{
Console.Write("Returning in catch. ");
return;
}
if (radioThrowInCatch.Checked)
{
Console.Write(" Throwing up in catch. ");
throw new Exception("after caught");
}
}
finally { Console.Write(" Finally!!"); }
Console.WriteLine(" Done!!!"); // before adding checkboxThrowInCatch,
// this would never happen (and was marked grey by ReSharper)
}
Output:
To summarize: Finally takes care of two things:
Finally to summarize "FINALLY": Finally does nothing special if you tried,and
And last but not least (finally): If you have an exception in your code that YOU DID NOT CATCH, your code will fly, WITHOUT REACHING THE FINALLY.
Hope this is clear. (Now it is to me...)
Moshe