[antlr-interest] AntLR C# target exceptions problem

Jan Newger Jan.Newger at rwth-aachen.de
Mon Apr 13 07:18:05 PDT 2009


Johannes Luber wrote:
>> Hi,
>>
>> I'm trying to get the C# target to work, only with partial success. I'm
>> using VS 2008 SP1 or VS 2005 with AntlrWorks and the runtime from
>> DOT-NET-runtime-3.1.2.zip.
>> The problem is, that exceptions raised due to invalid input don't
>> "bubble up" to my code. I noticed the paragraph "Exceptions not trapped
>> in Visual Studio" (see below) on the antlr website.
>> I took the test grammar from
>> http://www.antlr.org/wiki/pages/viewpage.action?pageId=557075 and used
>> the generated parser and lexer. My code looks like this:
>>
>> try
>> {
>> 	ANTLRFileStream fs = new ANTLRFileStream("test.txt");
>> 	TestLexer lex = new TestLexer(fs);
>> 	CommonTokenStream tokens = new CommonTokenStream(lex);
>> 	TestParser parser = new TestParser(tokens);
>> 	parser.expr();
>> }
>> catch (NoViableAltException ex)
>> {
>> 	MessageBox.Show(ex.Message);
>> }
>>
>> When I debug this code with VS (debug build) the debugger halts in the
>> Lexer with a NoViableAltException, but my code never gets a chance to
>> catch this exception, same goes for the release build. I even don't even
>> get an exception when no debugger is attached.
>>
>> Note that when i do what the entry "Exceptions not trapped in Visual
>> Studio" suggests, VS just continues after the exceptions, i.e. ignores
>> them, and my code also never has the chance to catch them.
>>
>> Any ideas?
> 
> Does putting a breakpoint into the catch block work? Otherwise the menu for exceptions offers the options to break when an exception is thrown and if an exception is caught. Furthermore, did you add a messagebox call or console print to check if the catch block is actually executed (in release mode)? Otherwise I can't check things out myself, as my development machine has a display problem and is thus unusable right now.
>

Putting a breakpoint into the catch block doesn't work. If you look at
the code I posted, there actually is a messagebox call, which is never
executed (debug or release mode doesn't matter).

I noticed however, that when a NoViableAltException is thrown from my
generated parser, that the parser itself catches it, calls ReportError
then Recover and ultimately returns silently from this method (start
symbol of my grammar). So in fact the exception is swallowed by the
parser itself. Is this "by design"? I just didn't expect this kind of
behavior, because it seems to be quite different from the java runtime
approach. What would be the solution? Should I override the ReportError
method in my parser and rethrow the exception?

Thanks and regards
Jan


More information about the antlr-interest mailing list