[antlr-interest] Problem catching exceptions in c# generated code

Ed Sykes ed.sykes at gmail.com
Thu Feb 21 06:16:33 PST 2008


Hi everyone,

first post here, so hello everyone :)

I've been using antlr with the c# generator for a month or so and i have a
problem that i now need to solve. The problem is that despite surrounding
the calls to the expression parser in a try catch i don't seem to be able to
catch the exceptions thrown. e.g.:

try
{
    MetricellKPIExpressionParser.expr_return result = parser.expr();
    CommonTree parsedTree = (CommonTree)(result.Tree);
}
catch(Exception ex)
{
    isValid = false;
}

will not catch any of the exceptions thrown by the parser, despite them
appearing to be derived ultimately (via RecognitionException) from
System.Exception:

namespace Antlr.Runtime
{
    [Serializable]
    public class RecognitionException : Exception
    {
        protected int c;
        protected int charPositionInLine;
    ...
}

Catching the derived exceptions or catching RecognitionExceptions doesn't
work either.

Up until now I have worked around this by using my own exception class
through the using declaration:

using NoViableAltException = MyCode.MyNoViableAltException;
using EarlyExitException = MyCode.MyEarlyExitException;

this is obviously less than ideal and I also can't use this trick on
exceptions thrown outside of the lexer and parser, for instance from the
antlr runtime via the Match() function.

I thought that perhaps the problem is originating from the fact that somehow
threading was being used in the antlr runtime but i checked and i can't see
any threads being spawned. I'm also new to c# (although have spent 8 years
with c++) so I am aware that there may be something i don't understand about
the clr and the way that it uses exceptions (although i have done a lot of
reading on that).

I'm at a bit of a loss as to why i can't catch the exceptions and I was
hoping that someone on the mailing list might have some ideas, i'm happy to
post up grammars etc should anyone be interested in trying to help me.

p.s. apart from this problem i'm finding antlr to be a great tool :)

thanks, Ed
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080221/62717f3d/attachment.html 


More information about the antlr-interest mailing list