dcsimg
Exception Handling
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Derek_Graham
Posted On:   Thursday, August 7, 2003 08:42 AM

Hi, I have a simple command interpreter and would like to know how to handle exceptions gracefully. I.e. When a user inputs an invalid command or invalid parameters to a command the program displays an error message and allows the user to continue as normal. The top-level rule in the parser part of the grammer file looks like: expr : command SEMI! ; exception catch [RecognitionException e] { System.err.println("Argh! A RecognitionException! (" + e + ")"); consumeUntil(SEMI); } catch [TokenStreamException e] { System.err.println("Argh! A TokenStreamException! (" + e + ")");    More>>

Hi,


I have a simple command interpreter and would like to know how to handle exceptions gracefully. I.e. When a user inputs an invalid command or invalid parameters to a command the program displays an error message and allows the user to continue as normal.


The top-level rule in the parser part of the grammer file looks like:

			
expr
:
command SEMI!
;
exception
catch [RecognitionException e] {
System.err.println("Argh! A RecognitionException! (" + e + ")");
consumeUntil(SEMI);
}
catch [TokenStreamException e] {
System.err.println("Argh! A TokenStreamException! (" + e + ")");
consumeUntil(SEMI);
}


I have defaultErrorHandler = false; in the parser and lexer, which means all error handling is propagated up to the catches above? So when i enter an incorrect command like "hello;" it continually spews out "Argh! A RecognitionException..." and i can do nothing but kill the program.


I'm guessing its because in my main program i have an infinite loop that calls parser.expr() ?


Does anyone know what i'm doing wrong or suggest a better way?


Thanks in advance,



Derek

   <<Less

Re: Exception Handling

Posted By:   Monty_Zukowski  
Posted On:   Friday, August 8, 2003 08:30 AM

If you look at code generated by the default error handling you will notice that it consumes one token of input so that it doesn't spawn an infinite loop like you are doing there.
About | Sitemap | Contact