dev@glassfish.java.net

Re: handling unexpected exceptions

From: Bill Shannon <bill.shannon_at_sun.com>
Date: Thu, 14 May 2009 00:14:33 -0700

Tim Quinn wrote:
> First, a question: do the methods invoked in the try block throw any
> checked exceptions?

Yes, sometimes they do.

> I guess FindBugs would not complain in that case -
> or perhaps FindBugs is complaining because the exceptions that are
> declared as thrown are subclasses of Exception and not Exception itself?

Right.

> Second, for what it's worth...
>
> I think the primary audience of source code is another human. If I were
> going to read this code, the clearest (to me) expression of the intent
> would be:
>
> try {
> // do something that might fail for unknown reasons
> } catch (Exception ex) {
> // do stuff that's needed only in an exception case
> throw MyException(ex); // chains the exception to avoid masking the
> lower-level cause
> } finally {
> // clean up, close connections, etc. that happens in either success or
> failure cases
> }

In my cases I only have cleanup to do in the failure case.
In the case of success, I *want* the connection left open.

> Adding the "success" variable to placate FindBugs turns us, the master,
> into the servant, with a sacrifice of clarity in the process.
>
> I say filter out this FindBugs objection - or change FindBugs so it does
> not complain about this in the first place.

Thanks for your advice.