This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception catch

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • 0
  • 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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (beginner).
 
Yu Fei Xu
Greenhorn
Posts: 8
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic