2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
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
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01: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
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 10:16:44 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2020-06-30 10:16:44 +02:00
----
throw std::logic_error("Unexpected null 'user_id' argument."); // Noncompliant
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 10:16:44 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2020-06-30 10:16:44 +02:00
----
throw std::invalid_argument("Unexpected null 'user_id' argument.");
----
2023-05-03 11:06:20 +02:00
== Resources
2020-12-21 15:38:52 +01:00
2022-04-07 08:53:59 -05:00
* https://cwe.mitre.org/data/definitions/397[MITRE, CWE-397] - Declaration of Throws for Generic Exception
2020-12-21 15:38:52 +01:00
* https://wiki.sei.cmu.edu/confluence/x/_DdGBQ[CERT, ERR07-J.] - Do not throw RuntimeException, Exception, or Throwable
2021-01-26 14:30:57 +01:00
* 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)
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 16 Aug 2016, 13:57:01 Ann Campbell wrote:
Looks good, [~alban.auzeill]
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]