Why is this an issue?
If you throw a general exception type, such as std::exception
, std::logic_error
or std::runtime_error
, it
forces consumers to catch all exceptions, including unknown exceptions they don’t necessarily know how to handle.
Instead, either throw a subtype that already exists ( for example in <stdexcept>
), or create your own type that derives from a
standard one.
Noncompliant code example
throw std::logic_error("Unexpected null 'user_id' argument."); // Noncompliant
Compliant solution
throw std::invalid_argument("Unexpected null 'user_id' argument.");
Resources