• Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use Try-Finally combination instead of Try Catch combination  RSS feed

 
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can use try catch combination and try finally combination is also allowed to be used. When to use Try-Finally combination instead of Try Catch combination? Which are some such cases.

thanks

 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two aren't mutually exclusive. You can use both finally and catch clauses with a try statement. you should use catch for handling exceptions when it's the appropriate time, and you use finally for performing cleanup that should happen always, regardless of how the try clause executes.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

What I am trying to understand is that why would someone choose to use Try Finally instead of try catch finally or try catch. Even if someone wants to use Try and Finally then why not also add catch block and log the exception.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it might be an unchecked Exception which you don't want to catch. Because you may have something which probably won't suffer any Exceptions but you need to be sure. If the Lock is not unlocked, the whole program will deadlock whenever it uses this Foo object:-
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!