During the process of language translation, the compiler can encounter errors. While the compiler might not always know the exact cause of the error, it can detect and analyze the visible problems. The main purpose of error handling is to assist the programmer by pointing out issues in their code.
Error handler = Error Detection + Error Report + Error Recovery.
Sources of Error in Error Handling
Errors can occur at different stages of the compilation process such as lexical, syntax, semantic, and code generation phases. The parser mainly detects syntax errors, but semantic errors like undeclared variables may also occur. For example, using an undeclared identifier can result in a missing entry in the symbol table. Error handling detects errors, reports them clearly, and applies recovery techniques. A good error handling mechanism allows the compiler to continue processing without significantly increasing compilation time.
There are two main types of errors:
In programming and compiler design, errors are broadly classified into two main types:
1. Run-Time Errors
These errors happen while the program is running. They usually occur because of issues like incorrect system settings or invalid input data. Examples of run-time errors include:
- Lack of memory to run the program.
- Memory conflicts with other programs.
- Logical errors, where the program doesn’t produce the expected results. These can be fixed by carefully debugging the code.
2. Compile-Time Errors
These errors occur before the program starts running, during the compilation process. They stop the program from compiling successfully.
Examples of compile-time include:
- Syntax errors (like missing semicolons or incorrect statements).
- Missing file references that prevent the program from compiling.
Finding error or reporting an error
Viable-prefix is the property of a parser that allows early detection of syntax errors.
- Goal detection of an error as soon as possible without further consuming unnecessary input
- How: detect an error as soon as the prefix of the input does not match a prefix of any string in the language.
Example: for(;), this will report an error as for having two semicolons inside braces.
Error Recovery
There are several methods that a compiler uses to recover from errors. These methods help the compiler continue processing the code instead of stopping immediately.
Common recovery methods include:
- Panic Mode Recovery – Skips erroneous code and resumes from the next valid statement.
- Phase-Level Recovery – Replaces small incorrect code segments with valid ones.
- Error Productions – Recognizes common errors and provides specific suggestions.
- Global Correction – Makes multiple changes to fix errors optimally.

