40 lines
1.3 KiB
Plaintext
40 lines
1.3 KiB
Plaintext
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
|
|
|
|
[source,cpp]
|
|
----
|
|
throw std::logic_error("Unexpected null 'user_id' argument."); // Noncompliant
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
[source,cpp]
|
|
----
|
|
throw std::invalid_argument("Unexpected null 'user_id' argument.");
|
|
----
|
|
|
|
== See
|
|
|
|
* https://cwe.mitre.org/data/definitions/397[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[{cpp} Core Guidelines E.14] - Use purpose-designed user-defined types as exceptions (not built-in types)
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|