Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we write a try catch block inside a finally block

 
Vivek Hingorani
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we write a try catch block inside a finally block and in any cases we write the same inside a finally block.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek, welcome to the Ranch!

About your question: Why don't you just try it out? Experimenting is the best way to learn things.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, you can nest them, however it can get a little confusing if you have too much of that going on, from my experience
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I add to Vivek's question? This is something I've been wondering about too. For some of the IO classes, the close() method can throw an exception. However, putting another try/catch block inside the finally block, or nesting try/catch/finally blocks in some other way, seems awkward. What is the preferred way of handling this situation?
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kurt Van Etten wrote:Can I add to Vivek's question? This is something I've been wondering about too. For some of the IO classes, the close() method can throw an exception. However, putting another try/catch block inside the finally block, or nesting try/catch/finally blocks in some other way, seems awkward. What is the preferred way of handling this situation?


I prefer to have only one source of try/catch in each method. Then I logically order them so that they either can take care of the Exception or throw it to the calling method to handle.

A typical such construction is when handling files. I have a method that handles the opening of the file and returns the File-instance if it is opened. If I get a File (sometimes I check for not null and sometimes I use an Exception, depends on that assignment) then I start the reading process. If the result from the reading is OK, then I go ahead and close the file. It might be a bit overkill and I have had quite some discussions about "duplicate code, since they throw the same exception", but it helps in the debugging when several calls in a call chain can throw the same exception.

Not a clear answer to your question...
 
Vivek Hingorani
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even i answered the same in an interview that i guess we can write but not sure of the use..But thinking logically any code in a finally block should not throw a exception and we should not complicate things....
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read about this new feature related to this discussion in JDK 7..

http://download.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic