On May 14, 2009, at 12:21 AM, Bill Shannon wrote:
> Craig L Russell wrote:
>> Hi Bill,
>> On May 13, 2009, at 5:39 PM, Bill Shannon wrote:
>>> I have a bunch of code in JavaMail that looks roughly like this:
>>>
>>> try {
>>> // do something that might fail for unknown reasons
>>> } catch (Exception ex) {
>>> // clean up, close connections, etc.
>>> throw MyException();
>>> // or...
>>> return null;
>>> // or...
>>> continue;
>>> }
>>>
>>> The "do something" can fail for lots of reasons - NPE,
>>> IOException, etc. -
>>> usually several levels deep. No matter what goes wrong I want to
>>> detect
>>> the failure and clean things up, especially making sure not to
>>> leave any
>>> connections open.
>>>
>>> FindBugs complains about this because "do something" doesn't throw
>>> Exception.
>>>
>> If do something doesn't throw Exception, you can catch
>> RuntimeException or Throwable depending on whether you're prepared
>> for just RuntimeException or Error conditions.
>
> Often "do something" throws some checked exception, so I need to
> handle
> it *and* the possible RuntimeExceptions.
This seems to be the crux of the FindBugs complaint.
So IOException gets wrapped in RuntimeException which catch
(RuntimeException) would catch and FindBugs would not complain.
>>
> If a Throwable-but-not-Exception occurs I figure everything's going
> down in flames anyway and I don't worry about it. (Usually. I'm not
> as consistent as I would like. :-))
>
>> And as Tim points out, you can clean up things in a finally block
>> regardless of whether an exception was thrown. Just don't "return"
>> in the finally block because that will obscure an exception that
>> was thrown.
>
> I only need to do cleanup in the failure case.
>
> In the cases where I would return, I return null or some other value
> that indicates the failure. I purposely hide the low level exception
> from the upper level code, although more often I do that by wrapping
> it in my own exception.
Which is, of course, what "do something" is also doing.
Craig
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>
Craig L Russell
Architect, Sun Java Enterprise System
http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell_at_sun.com
P.S. A good JDO? O, Gasp!
- application/pkcs7-signature attachment: smime.p7s