Error handling
E.18
Minimize the use of explicit `try`/`catch`
Reason
try/catch is verbose and non-trivial uses are error-prone. try/catch can be a sign of unsystematic and/or low-level resource management or error handling.
Example, Bad
void f(zstring s)
{
Gadget* p;
try {
p = new Gadget(s);
// ...
delete p;
}
catch (Gadget_construction_failure) {
delete p;
throw;
}
}
This code is messy. There could be a leak from the naked pointer in the try block. Not all exceptions are handled. deleting an object that failed to construct is almost certainly a mistake. Better:
void f2(zstring s)
{
Gadget g {s};
}
Alternatives
Enforcement
??? hard, needs a heuristic