[antlr-interest] Exceptions and finally blocks

Terence Parr parrt at cs.usfca.edu
Sun Mar 12 11:41:20 PST 2006


Yep, was thinking that or wrapping exception or someting...i like  
this pending thing.thanks
T
On Mar 12, 2006, at 11:38 AM, Scott Stanchfield wrote:

> heheh... Fun stuff. The trick is that Java only allows one pending
> exception. I usually handle it something like this (useful for jdbc  
> close,
> for example)
>
> Throwable pendingThrowable = null;
> Connection connection = null;
> try {
>   try {
>     connection = ... // open connection
>     // do database stuff
>   } catch(Throwable t) {
>     pendingThrowable = t;
>   }
>
> } finally {
>   try {
>     if (connection != null) {
>       connection.close();
>     }
>   } catch(Throwable t) {
>     if (pendingThrowable != null) {
>       throw pendingThrowable;
>     }
>     throw t;
>   }
> }
>
> Lovely, ain't it? Gotta fix this when I write my language...
>
> Hope this helps a bit...
> -- Scott
>
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org
>> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
>> Sent: Sunday, March 12, 2006 2:23 PM
>> To: antlr-interest Interest
>> Subject: [antlr-interest] Exceptions and finally blocks
>>
>> hi,
>>
>>   I'm running into this very strange Java behavior, which I believe
>> is considered normal but it does not give us what we want.   when an
>> exception is thrown in a method it still tries to execute the
>> finally of course. However, when an exception occurs usually
>> it means that this stuff in the finally clause will not
>> execute without its own exception. This second exception
>> forces the first exception to be lost. It is very confusing
>> because you see an exception in the finally when all of those
>> variables should be okay but you get null pointer.
>>
>>   should I add the following:
>>
>> catch (Exception e) {
>> ..handle exception..perhaps noUserFinally=true; throw e; } finally {
>>    usual cleanup
>>    if ( !noUserFinally ) { user stuff }
>> ...
>> }
>>
>> Hmm.... this is turning out to be a serious problem for me as I
>> developed my Mantra language with version three. Very very
>> annoying.  Suggestions?
>>
>> Ter
>>
>
>



More information about the antlr-interest mailing list