Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

exception catch

 
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)
 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (beginner).
 
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
 
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.
 
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.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic