This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exception catch  RSS feed

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (beginner).
 
Yu Fei Xu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,in code A the "jdbcConnection.close()" be executed .
but in code B ,I am not sure
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the call to close will execute in both of your provided cases.
 
Vinayak patil
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!