Home arrow static arrow Java Programming [Archive] - Catching causal exceptions from finally{} block?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Catching causal exceptions from finally{} block?
This topic has 84 replies on 6 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | 6 | Next »

Posts:19,725
Registered: 9/26/01
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 8:46 AM (reply 30 of 84)



 
I was sort of thinking the same thing when I opted to
shortcut it, but then, I realized I couldn't be sure
enough of that to really justify the position. Each
one really should have its own try/catch. I would
think that probably one of them throwing an
exception would indicate you're not going to get
proper cleanup from the others, but it's so simple to
apply due diligence here.

But this is ugly, no?
finally {
try {
rs.close();
}
catch(SQLException e) {
log.error("close error", e);
}
try {
stmt.close();
}
catch(SQLException e) {
log.error("close error", e);
}
...
 

Posts:37,103
Registered: 3/30/99
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 8:48 AM (reply 31 of 84)



 
But this is ugly, no?

Yes it is. If only there were some way in computer programming to take ugly code that gets reused often, stick it off on the side in its own little world, and then invoke it by some cleaner, simpler, less ugly means. If only there were a method to do such a thing. :-)
 

Posts:19,725
Registered: 9/26/01
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 8:50 AM (reply 32 of 84)



 
Yes it is. If only there were some way in computer
programming to take ugly code that gets reused often,
stick it off on the side in its own little world, and
then invoke it by some cleaner, simpler, less ugly
means. If only there were a method to do such a thing.
:-)

Ok, smarty-pants.
 

Posts:19,725
Registered: 9/26/01
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 8:51 AM (reply 33 of 84)



 
Forgot the winky ;-)
 

Posts:37,103
Registered: 3/30/99
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 8:54 AM (reply 34 of 84)



 
When ever somebody mentions my pants, I assume the winky is implied.
 

Posts:13,769
Registered: 00-11-29
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 9:05 AM (reply 35 of 84)



 
Not to get it to compile, no. But to get correct
behavior, you must put a try/catch inside finally.

You realize this is the point of my post, right?
 

Posts:37,103
Registered: 3/30/99
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 9:20 AM (reply 36 of 84)



 
Not to get it to compile, no. But to get correct
behavior, you must put a try/catch inside finally.

You realize this is the point of my post, right?

No, I didn't realize that. Thanks for clarifying.
 

Posts:24,036
Registered: 2/3/03
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 9:25 AM (reply 37 of 84)



 
Actually you need a try/catch around each of the
close
methods.

Yeah, I know. I was shortcutting it for clarity's
sake, but I figured I'd regret it. In real life, I
have a helper class that does the try/catch/log for
closing DB stuff, files, sockets, etc. Keeps the
finally blocks cleaner.

Jakarta Commons has some utility classes that do a fine job of this, too, if anyone's interested.

Yawmark,
The Jakarta Commons Spammer...
 

Posts:1,577
Registered: 00-02-25
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 11:12 AM (reply 38 of 84)



 
Yes it is. If only there were some way in computer
programming to take ugly code that gets reused often,
stick it off on the side in its own little world, and
then invoke it by some cleaner, simpler, less ugly
means. If only there were a method to do such a thing.

Or a procedure that we could follow :-)

One serious approach is to use the Command pattern to invert responsibility for managing resources. For example, take the following database code ...
Connection cxt = null;Statement stmt = nulltry {    cxt = // however you get a connection    stmt = cxt.createStatement();    // and so on}catch (Exception e) {    // log it, convert it to something more appropriate, whatever}finally {    // wrap these in try/catch if you remember    stmt.close();    cxt.close();}

There's a lot of code here that's common to all database operations, and moreover, it's easy code to screw up (such as not remembering the try/catch around close()). As an alternative, define the following interface:
public interface DbOperation{    perform( Connection cxt, Statement stmt );}

Instances of this interface are then passed to a "connection manager" object, which contains all the code to get a connection, commit/rollback depending on whether the operation threw an exception, and clean up afterward.
 

Posts:2,830
Registered: 9/1/03
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 2:52 PM (reply 39 of 84)



 
Huh? That doesn't solve the problem, that just hides
it. Checked exceptions or no, you don't want cleanup
to change how your method completes. If all exceptions
were unchecked, you could successfully insert or
retrieve from the DB, then close could throw an
unchecked exception, and the whole method would then
throw the exception, even though it did its work
successfully.

no comment ? :) (but actually in this case checked exceptions are hiding
the problem, if they were not checked, then they originally exception would have
been observed.).
 

Posts:2,830
Registered: 9/1/03
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 2:53 PM (reply 40 of 84)



 
no comment ? :) (but actually in this case checked
exceptions are hiding
the problem, if they were not checked, then the
original exception would have
been observed.).
 

Posts:6,750
Registered: 1/25/04
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 3:03 PM (reply 41 of 84)



 
Whether the exceptions are checked or no, you have to try-catch to properly 1) close resources and 2) report exceptions.
 

Posts:2,830
Registered: 9/1/03
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 3:14 PM (reply 42 of 84)



 
Whether the exceptions are checked or no, you have to
try-catch to properly 1) close resources and 2) report
exceptions.

you should but you don't have to. and with non-checked you can
do all this at the location you deem appropriate, with checked the
location is forced upon you.

in this case the lack of checked would've meant the first exception
would be propagated, not smothered.
 

Posts:13,769
Registered: 00-11-29
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 3:24 PM (reply 43 of 84)



 
you should but you don't have to. and with non-checked
you can
do all this at the location you deem appropriate, with
checked the
location is forced upon you.

But you said you wouldn't need a finally block. That's crazy. Whether the exception is checked or not, it's still thrown at the point it's encountered. Whether the exception unchecked has nothing to do with whether you need a finally block.
 

Posts:2,830
Registered: 9/1/03
Re: Catching causal exceptions from finally{} block?  
Aug 3, 2004 3:28 PM (reply 44 of 84)



 
you should but you don't have to. and with
non-checked
you can
do all this at the location you deem appropriate,
with
checked the
location is forced upon you.

But you said you wouldn't need a finally block.
That's crazy. Whether the exception is checked or
not, it's still thrown at the point it's encountered.
Whether the exception unchecked has nothing to do
o with whether you need a finally block.

of course, i'm not suggesting that :). but this error has come due to the
requirement to catch checked exceptions. if the user was allowed to
put his exception-handling code in as he pleased, it probably wouldn't
have been so crappy.
 
This topic has 84 replies on 6 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | 6 | Next »