[antlr-interest] Exceptions and finally blocks

Terence Parr parrt at cs.usfca.edu
Sun Mar 12 12:53:15 PST 2006


On Mar 12, 2006, at 11:38 AM, Scott Stanchfield wrote:
> 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...

Ok, tried this out and Java makes me put "throws Throwable" on EVERY  
FREAKIN METHOD.  Literally...almost all runtime methods can  
eventually call something that might throw an exception.  Java makes  
me say "throws Throwable" so we're sunk...i'm not putting that on  
every method in my program.  Stupid java.

ok, i'm going to simply allow the user to specify default catch stuff  
for rules:

@rulecatch {
catch (...) {...}
catch (...) {...}
}

The user can add that to the grammar and it will override all rule  
catch stuff.  As you can see the exceptions on a rule will override  
the default.

<if(exceptions)>
     <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}>
<else>
<if(!emptyRule)>
<if(actions.(actionScope).rulecatch)>
<actions.(actionScope).rulecatch>
<else>
     catch (RecognitionException re) {
         reportError(re);
         recover(input,re);
     }<\n>
<endif>
<endif>
<endif>

Is rulecatch the right name?

Ter


More information about the antlr-interest mailing list