Custom visitors and throwing excpetions

30 views
Skip to first unread message

Bruce Thompson

unread,
Jul 23, 2015, 4:36:32 PM7/23/15
to SableCC
Seems like a common issue but can't find it anywhere....

I am writing a compiler that is built into a much larger software package using SableCC (which rocks by the way).  In this case, I can't simply call System.exit() when one of my custom visitors finds a problem.  I need to do something like throw an exception.  However, the method signatures can't appear to be modified by adding throws to it (at least I couldn't get that to work).  About the only way I've seen is to simply use a RuntimeException and throw that since it doesn't require the class method to include a throws entry.

Is there a better, normal, best practice on how to do this?  When I call things like ast.apply(new SemanticAnalysis());  I want to be able to catch an exception and report this to the calling methods in the rest of the system.

Hope this made sense...

Thanks in advance...

Bruce

Niklas Matthies

unread,
Jul 23, 2015, 5:48:27 PM7/23/15
to sab...@googlegroups.com
Bruce,

Most users probably just use runtime exceptions. You can patch the
code templates (*.txt files) within sablecc.jar to provide support for
checked exceptions (and return values). The other alternative is to
store error information in the visitor object and have the calling
code query it. Personally I use the patching option, primarily to have
return values, as I vastly prefer stateless visitor objects with a
functional programming style.

Niklas

Bruce Thompson

unread,
Jul 23, 2015, 5:55:57 PM7/23/15
to SableCC, bru...@expeditefile.com
Thanks Niklas, I appreciate your speedy reply.  Yea, I think for now I will pay with the runtime exception stuff but may have to look a bit more closely at the code templates idea...

Bruce

Bruce Thompson

unread,
Jul 23, 2015, 5:57:41 PM7/23/15
to SableCC, bru...@expeditefile.com
Ops!  Should be "play" rather than "pay"!

Bruce
Reply all
Reply to author
Forward
0 new messages