It took me a few years to get this Finally{}.
try
{
DoA(); //known to throw an exception
}
catch(Exception ex)
{
LogError(ex);
}
DoC();
Whether that DoC() is in a Finally or not, it would be called. So I never could understand the use of that finally.
Yesterday, I had it ! I asked my great friend Michel Prevost. The finally{} exists to perform actions regardless of any “weird” actions in that catch{}. And by “weird” I mean a “throw” or a “return“.
try
{
DoA(); //known to throw an exception
}
catch(Exception ex)
{
LogError(ex);
throw new Exception("doh");
}
finally
{
DoB();
}
DoB() will be executed, even though an error was thrown in the catch. If a “return” was called instead of the throw new, DoB(); would have been called too.
It creates weird and unnatural flow of execution, but it makes the releasing of objects in an organized manner.
I bet that if I look back at the documentation, everything will be evident !
I learn fast if get long explanations :)
Pat