Error handling prelude
"From then on, when anything went wrong with a computer, we said it had bugs in it."
- Grace Hopper
Writing programs that behave well under expected conditions is a good start. It's when a program encounters unexpected situations where it gets really challenging. Proper error handling is an important but often overlooked practice in software development. Most error handling, in general, falls into three categories:
- Recoverable errors that are expected to happen due to the user and the environment interacting with the program, for example, a
file not found
error or a number parse error. - Non-recoverable errors that violate the contracts or invariants of the program, for example,
index out of bounds
ordivide by zero
. - Fatal errors that abort the program immediately. Such situations include running out of memory, and stack overflow.
Programming in the real world often entails dealing with errors. Examples include malicious input to a web application, connection failures in network...