If the lookahead predicts no alternative of a production in either the parser or lexer, then a NoViableAltException is thrown. Otherwise, parser exceptions must be simulated by the code generator in a more laborious fashion. Namely, when an exception is thrown, the normal thread of execution is stopped, and functions on the call stack are exited sequentially until one is encountered that wants to catch the exception. When an exception is caught, execution resumes at that point.
Exception handlers arise in one of two ways.
The default exception handler will report an error, sync to the follow set of the rule, and return from that rule. Second, you may specify your own exception handlers in a variety of ways, as described later. If you specify an exception handler for a rule, then the default exception handler is not generated for that rule. You may attach exception handlers to a rule, an alternative, or a labeled element. Matching and control flow continues as if the error had not occurred.