File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception catch" Watch "exception catch" New topic

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
} finally {

case B:
try {

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

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

Joined: Jan 10, 2002
Posts: 63868

Moved to Java in General (beginner).

[Asking smart questions] [About Bear] [Books by Bear]
Yu Fei Xu

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"
I agree. Here's the link:
subject: exception catch
It's not a secret anymore!