tags:

views:

135

answers:

6

I feel puzzle ...

I write a small routine in .jsp. Finally, ResultSet, Statement and Connection are required to be closed. I also write the closing codes in finally { }, but when the page is run, it return error that I didn't catch exception ...

I read some forum. Other people didn't catch any exception in finally { }

Any Hint ?

+1  A: 

finally{} doesn't do any exception catching. A finally{} block exists to make sure that certain code is run, no matter whether the try{} block reached its natural end or if it's jumping temporarily to the finally{} because an exception happened and that finally{} block was along the way. But after the finally{} finishes, the exception goes about its merry business, cavorting its way up the stack and cheerfully crashing your program.

If you want to actually catch the exception and stop it from unwinding the stack further, use catch(){}. But don't use catch blindly- catching an exception you don't actually know how to recover from is much worse than crashing, because now your program isn't working correctly and you don't have an exception stack trace telling you why.

Your ResultSet, Statement, and Connection almost certainly did get closed. And then the exception continued happening and crashed your program anyway, because that had nothing to do with your ResultSet, Statement, and Connection.

What was the actual exception?

Kistaro Windrider
+3  A: 

You must catch exceptions in the code finally block. As you must catch exceptions in the catch block. Nested try/catches are a regular thing (albeit ugly).

One important note here is that you could have the exceptions that occur in finally declared in the throws clause of the method. However that would lead to the exception in finally overriding the original exception, which is lost. And you will see, for example, a NullPointerException, rather than FileNotFoundException.

By the way, avoid having code in the JSP file. Place it in a servlet.

Bozho
+1  A: 

Maybe I'm getting old, but what's wrong with catching exceptions in the catch block?

Tom
A: 

It helps if you say what is in your try block. You are probably not catching appropriate exception or your code in finally throws exception.

It is OK to have finally without catch.

try {
 //do some work
}  

finally {
 //check of state and do clean up. You would have reached here via multiple branches. 
}

It more appropriate to catch specific exceptions using catch and then handle specific cleanup there. Use finally for any code that must get executed even when exception happen.

try {
   //do some work
}  
catch ( RecoverableException1 re1) {
   //cleanup
}
catch ( RecoverableException2 re2) {
   //cleanup
}

finally {
   //check of state and do clean up. You would have reached here via multiple branches.
}
Jayan
+2  A: 

Sounds like you have the old problem of needing to close() in a finally block but close() throws an exception itself. Try somethig like the following...

ResultSet rs;

try {
    // do various stuff
    rs = ...;

} finally {
    try {
        if (rs != null) rs.close();

    } catch (SQLException e) {
        // do something with exception
    } 
}
Michael Rutherfurd
A: 

finally{ try{ resultSet.close(); }catch(E e){ }finally{ try{ statement.close(); }catch(E e){ }finally{ conn.close(); } }

}