Re: finally block does not complete normally - Eclipse
- From: asb@xxxxxxxxxxxxxx (Antti S. Brax)
- Date: 7 Jun 2005 10:45:15 GMT
cdsmith@xxxxxxx wrote in comp.lang.java.programmer:
> Antti S. Brax <asb@xxxxxxxxxxxxxx> wrote:
>> Please don't forget to put a try-catch block inside the finally
>> block (this is very important in example code because people use
>> it to learn things).
>
> I strongly disagree that it's a good idea to put a try/catch inside the
> finally.
>
> After all, what do you intend to do with the exception when you catch
> it? Are you saying that the code that reads the file ought to have
> knowledge of the exception handling strategy for the software?
Hell yeah! It would be silly to demand that a software does not
know it's own exception handling strategy.
<snip>
> Too often, people taking this kind of advice -- to always handle
> exceptions -- do something ineffective, like just write it to standard
> output then drop it.
You are generalizing a very specific issue here (but I agree
with your generic advice anyway).
> The problem is that if finally executes because
> the original code completed normally, then you miss proper handling of
> the exception and it very likely gets forgotten. That leads to
> undetected faults in cases like running out of disk space while flushing
> the buffers at the end of a write operation.
I see that you have a point and I agree partly. But how exactly
should the exception handling be done in the finally block so
that no important information is hidden from the caller? I came
up with a solution, but it is quite ugly and not at all perfect
(exception thrown in finally is still "hidden" if an exception
is thrown in try).
fos = new FileOutputStream(FILENAME);
boolean errors = true;
try {
// Manipulate output stream.
} finally {
try {
fos.close();
} catch (IOException ex) {
if (errors) {
// Log exception.
} else {
// Rethrow the exception.
}
}
}
Since it is likely (as you also say below) that whatever is
thrown in the close might be caused by the same error that
caused the earlier exception, it is more important to pass
the earlier exception to the caller.
> Generally speaking,
> though, the two exceptions will be caused by EXACTLY the same thing, and
> having just one of the two exceptions for debugging is good enough.
No. The earlier exception is much more important, since it
also contains information about the state in which the (please
forgive me) "business logic" was when things went wrong. This
is usually quite important especially if fixing the problem
involves cleaning up things that were left in an invalid state.
--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/
[1385 messages expunged from folder "Spam"]
.
- Follow-Ups:
- Re: finally block does not complete normally - Eclipse
- From: Chris Smith
- Re: finally block does not complete normally - Eclipse
- References:
- Re: finally block does not complete normally - Eclipse
- From: enrique
- Re: finally block does not complete normally - Eclipse
- From: Mike Schilling
- Re: finally block does not complete normally - Eclipse
- From: Antti S. Brax
- Re: finally block does not complete normally - Eclipse
- From: Chris Smith
- Re: finally block does not complete normally - Eclipse
- Prev by Date: Re: resultSet is null
- Next by Date: Re: Fast search for a number within tolernaces
- Previous by thread: Re: finally block does not complete normally - Eclipse
- Next by thread: Re: finally block does not complete normally - Eclipse
- Index(es):
Relevant Pages
|