Error handling
E.31
Properly order your `catch`-clauses
Reason
catch-clauses are evaluated in the order they appear and one clause can hide another.
Example, bad
void f()
{
// ...
try {
// ...
}
catch (Base& b) { /* ... */ }
catch (Derived& d) { /* ... */ }
catch (...) { /* ... */ }
catch (std::exception& e) { /* ... */ }
}
If Derivedis derived from Base the Derived-handler will never be invoked. The "catch everything" handler ensured that the std::exception-handler will never be invoked.
Enforcement
Flag all "hiding handlers".
Con: Constants and immutability
You can't have a race condition on a constant. It is easier to reason about a program when many of the objects cannot change their values. Interfaces that promise "no change" of objects passed as arguments greatly increase readability.
Constant rule summary: