Error handling
Introduction / Definition
Error handling detects, reports, recovers from, or propagates abnormal conditions. It prevents unexpected failures from becoming silent or uncontrolled. Languages use exceptions, error values, result types, or error codes. Resource cleanup is important when errors occur.
Syntax / Structure / Formula
try { operation(); } catch(Exception e) { handle(e); }
Detailed Explanation
A good error policy identifies where errors are detected and handled. Errors should retain useful context rather than being silently discarded.
Example
Example: A file operation catches an I/O error and reports that the file could not be opened.
Output / Result
The failure is communicated rather than silently ignored.
Important Points / Notes
- Do not hide errors.
- Preserve useful context.
- Clean up resources.
Mini Practice Question
Why should errors not be silently ignored?