rspec/rules/S112/cfamily/rule.adoc

42 lines
1.3 KiB
Plaintext
Raw Normal View History

== 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
=== 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
----
=== 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.");
----
== Resources
* 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
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)
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[]