exceptions
try {
throw std::runtime_error("boom");
} catch (const std::runtime_error &e) {
check(std::string(e.what()) == "boom", "catch");
}
Catch by const reference
Catching by value slices derived exceptions and copies
Catching by non const reference allows accidental mutation
const & is the default
What to throw
Throw types, not strings
Derive from std::exception or use a standard exception type
Keep the message useful to the person reading logs at 3AM
What not to do
Do not use exceptions for ordinary control flow
Do not throw across a destructor boundary you do not control
Do not swallow everything with catch (...) unless you log and rethrow
noexcept
Mark functions noexcept only when they truly cannot throw
A throwing noexcept function terminates the process
That is a real boundary, not a hint
Next
Move semantics in 11 move
Posted