wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes exception catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception catch" Watch "exception catch" New topic
Author

exception catch

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
if I have the following code

case A:
*****************************
try {

// do some stuff
} catch (IOException) {
// handle error
response.sendRedirect("/errorPage.jsp");
} finally {
jdbcConnection.close();
}
********************************

case B:
try {

// do some stuff
} catch (IOException) {
// handle error
return "IOException";
} finally {
jdbcConnection.close();
}
****************************************

My question is --- Will the "jdbcConnection.close()" be executed (i.e. closing the jdbc Connection) in both Case A and B when there is IOException raised ? (One is redirecting page and one is returning out of subroutine)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Moved to Java in General (beginner).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Yu Fei Xu
Greenhorn

Joined: Mar 15, 2006
Posts: 8
yes,in code A the "jdbcConnection.close()" be executed .
but in code B ,I am not sure
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Yes, the call to close will execute in both of your provided cases.


Tony Morris
Java Q&A (FAQ, Trivia)
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
finally is called irrespective of whether you return or not.So the connection is closed.

If finally is not called, you would have got an unreachable code error at compile time.


"I can resist everything except temptation"
 
jQuery in Action, 2nd edition
 
subject: exception catch
 
Similar Threads
Excetion and finally block
catch block.....
One more on exceptions
Can we use finaly block to catch exceptions without using catch block?
How long time can I keep a database connection open?