Details
Description
Title
Exception specifications should not be used
Description
<p> Specifying which exceptions can be thrown by a function seems like a good idea. It can enable the compiler to verify that no other exception is being thrown, and generate more optimal code based on this knowledge. </p> <p> However, C++ defers those verification to be done at runtime instead of compile-time. When the verification fails, <code>std::unexpected()</code> is called and the program is likely to be terminated. </p> <p>The following code illustrates this rule:</p> <pre> #include <iostream> void f() throw(); /* Non-Compliant - Declares a function f, with the promise that it will not throw any exception */ void f() throw() /* Non-Compliant */ { throw 0; /* But the actual implementation does throw something, yet the code compiles */ } int main() { f(); /* At runtime, the throw is detected, and the program is terminated */ /* Unreachable code */ std::cout << "Hello, world!" << std::endl; return 0; } </pre>
Message
Remove this exception specification.
Severity
Major
In Sonar way?
Yes
Attachments
Issue Links
- implements
-
RSPEC-2303 Exception specifications should not be used
- Active
- relates to
-
CPP-388 Update description of rule: Exception specification should not be used
-
- Closed
-