rspec/rules/S112/cfamily/rule.adoc

22 lines
1.0 KiB
Plaintext
Raw Normal View History

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.
2020-06-30 10:16:44 +02:00
Instead, either throw a subtype that already exists ( for example in `+<stdexcept>+` ), or create your own type that derives from a standard one.
2020-06-30 10:16:44 +02:00
== Noncompliant Code Example
----
throw std::logic_error("Unexpected null 'user_id' argument."); // Noncompliant
----
== Compliant Solution
----
throw std::invalid_argument("Unexpected null 'user_id' argument.");
----
== See
* http://cwe.mitre.org/data/definitions/397.html[MITRE, CWE-397] - Declaration of Throws for Generic Exception
* https://wiki.sei.cmu.edu/confluence/x/_DdGBQ[CERT, ERR07-J.] - Do not throw RuntimeException, Exception, or Throwable
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#Re-exception-types[C++ Core Guidelines E.14] - Use purpose-designed user-defined types as exceptions (not built-in types)