Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finally block not being executed 1

Status
Not open for further replies.

odenc

Programmer
Sep 16, 2005
9
IE
Hi All,
I am debugging some java code, and have noticed that the finally block is not being executed.
As far as I am aware, in a try-catch-finally declaration, the finally part should always be run.
Is this the case?
And if so, does anybody have any idea why this block of code is being skipped over?
Kind regards,
Caroline
 
Post the code (at least the try/catch/finally part of it).
 
try {
sql = SOMESQL STATEMENT
results = this.getDbManager().executeSelect(sql);
if (results == null) {
throw new RecordNotFoundException("Error Retrieving record for" + aUserId);
} else
if (results.next()) {
incorrectLogins = results.getString("COUNTER");
}
} catch (SQLException ex) {
ex.printStackTrace();
throw new RecordNotFoundException("Error Retrieving COUNTER for " + aUserId);

} finally {
try {
if (results != null)
results.close();
} catch (SQLException ex) {
incorrectLogins = "N";
}
}

So in this particular block, no part of the finally clause is ever hit. After the try clause is executed it leaves the method, so the resultset is never closed. I think this is causing a problem in my application.
Thanks in advance for any help,
Caroline
 
What evidence do you have that the finally-block is not being hit/result set is not closing?
 
If results is null, the exception is thrown and the finally block won't be executed. In case of any other non-QL exception the result will be the same.

In other cases, the finally block will be executed. I'd add some println statements to make sure it's ok.

Cheers,
Dian
 
You say that when results is null, the exception is thrown. Therefore, the finally block WILL be executed, but nothing will happen since the if-conditional (results != null) is false.

Did you add those println statements? What did you discover?

--Dave
 
A 'finally' block is ALWAYS executed - even if you throw an exception not handled by the catch ...

I can assure you that the 'finally' block is being executed - your problem MUST lie elsewhere ...

BTW, throwing away exceptions is a bad idea - even if you don't do anything with it, you should ALWAYS IMO print out the exception - the data may prove invaluable in a live debug situation.

Add :

} finally {
try {
if (results != null)
results.close();
} catch (SQLException ex) {
ex.printStackTrace(System.err);
incorrectLogins = "N";
}
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Omg, I'm a java sinner.

Sedj is rigth, the finally block is always executed. Dammit, I've done that zillions of times befores.

I must be getting old.

Sorry for the confusion.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top