[antlr-interest] I want to throw an exception and stop parse, please!

Tilman Bender tbender at stud.hs-heilbronn.de
Sun May 31 03:21:53 PDT 2009


Hi Kevin,

Yes you can, but it is a bit of a dirty hack:
Make your custom Exception subclass RuntimeException.
This has two effects:

- The compiler will allow you to throw it in the recover method  
allthough recover has no throws declaration (RuntimeExceptions are  
unchecked)
- You cannot directly catch MyException in the code that invoked your  
parser, since the compiler will complain, that none such Exception is  
declared to
be thrown.

So that means:  Subclass RuntimeException and in the code calling your  
parser catch RuntimeException and gracefully exit your compiler/ 
interpreter/whatever.

HTH

Tilman Bender
Student des Software Engineering
Hochschule Heilbronn
tbender at stud.hs-heilbronn.de



Am 31.05.2009 um 08:21 schrieb Kevin Chen:

> But, there's no throws in original recover method:
>
>     public void recover(IntStream input, RecognitionException re)
>
> Thus, I can't throw an exception in overriding recover method. :-)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090531/c3618b54/attachment.html 


More information about the antlr-interest mailing list