Error handling
E.28
Avoid error handling based on global state (e.g. `errno`)
Reason
Global state is hard to manage and it is easy to forget to check it. When did you last test the return value of printf()?
See also: Simulating RAII
Example, bad
int last_err;
void f(int n)
{
// ...
p = static_cast<X*>(malloc(n * sizeof(X)));
if (!p) last_err = -1; // error if memory is exhausted
// ...
}
Note
C-style error handling is based on the global variable errno, so it is essentially impossible to avoid this style completely.
Enforcement
Awkward.