65.9K
CodeProject is changing. Read more.
Home

Try - finally alternative

starIconstarIconstarIconstarIconstarIcon

5.00/5 (5 votes)

Jun 3, 2011

CPOL
viewsIcon

8760

Not really, because what if your finally block would contain more lines of code than the single line you have in your example? That would make it already much harder to read.Secondly, you are misusing the Dispose method. You shouldn't invoke anything in it, especially not the Action, you...

Not really, because what if your finally block would contain more lines of code than the single line you have in your example? That would make it already much harder to read.

Secondly, you are misusing the Dispose method. You shouldn't invoke anything in it, especially not the Action, you should only be disposing used objects in it.

Also, where is the catch? When you are defining a try-block, you will need a catch in a proper design.

So in my opinion, this stays better:

try 
{
}
catch(Exception ex) 
{
}
finally 
{
}

You could make it a Fluent interface like this:

Try( () => Console.WriteLine("test") ).Catch((Exception ex) => 
    Console.WriteLine(ex.Message)).Finally(() => Console.WriteLine("Done!"));

But I don't think that's more readable either..