File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can we write a try catch block inside a finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can we write a try catch block inside a finally block" Watch "Can we write a try catch block inside a finally block" New topic
Author

Can we write a try catch block inside a finally block

Vivek Hingorani
Ranch Hand

Joined: Jul 08, 2011
Posts: 38
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

Vivek, welcome to the Ranch!

About your question: Why don't you just try it out? Experimenting is the best way to learn things.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Sure, you can nest them, however it can get a little confusing if you have too much of that going on, from my experience


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
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

Joined: Mar 10, 2008
Posts: 326

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

Joined: Jul 08, 2011
Posts: 38
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

Joined: Dec 17, 2009
Posts: 133
Read about this new feature related to this discussion in JDK 7..

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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we write a try catch block inside a finally block