Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Try - finally alternative

, 5 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

JV9999
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
GeneralMy thoughts exactly! PinmemberNaerling3-Jun-11 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 5 Jun 2011
Article Copyright 2011 by JV9999
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid