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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we have nested finally block ?" Watch "Can we have nested finally block ?" New topic
Author

Can we have nested finally block ?

raj chopra
Ranch Hand

Joined: Apr 09, 2012
Posts: 58
Hi All,

few parts of my code have too many open connections. so i plan to close them where necessary .

my dialama was that shall i go ahead with nested finally block at each try block respectively or to go with single finally block to close all the connections ?

please guide me to right steps in doing so .

below i present sample code for closing the connections

/


Reagrds,
raj chopra
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
Yes and no.
You cannot nest a finally inside another finally. You can however nest a try-finally inside a finally. In Java7, they introduced try with resources so as to obviate the need for such strange syntax.
raj chopra
Ranch Hand

Joined: Apr 09, 2012
Posts: 58
Thanks for your reply .

Campbell Ritchie wrote:Yes and no.
You cannot nest a finally inside another finally. You can however nest a try-finally inside a finally. In Java7, they introduced try with resources so as to obviate the need for such strange syntax.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
You’re welcome

Please only use the quote button if you are commenting about a particular part of the previous post; in that instance you are simply duplicating it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we have nested finally block ?
 
Similar Threads
JDBC Help figuring it out
Enabled Web start in NetBeans, but lost database connection
what is the scope of JSP objects ???
set ajax response in parent window field without refreshing the parent window
Are there any problems using the same statement for multiple queries?