MISRA C++:2023 Rule 18.1.2
An empty throw shall only occur within the compound-statement of a catch handler
Since R2024b
Description
Rule Definition
An empty throw shall only occur within the compound-statement of a catch handler.
Rationale
When you use an empty throw statement (throw;)
, the compiler checks
if an exception object is present in the current scope. If the current scope contains an
exception object, the compiler raises a temporary object containing the current exception.
If the current scope does not contain an exception objects, the compiler invokes
std::terminate()
implicitly. The function
std::terminate()
terminates the program execution in an
implementation-defined manner. That is, the exact process of program termination depends on
the software and hardware that you are using. For instance,
std:terminate()
might invoke std::abort()
to
abnormally abort the execution without unwinding the stack, leading to resource leak and
security vulnerabilities.
The best practice is to use an empty throw statement only in the
catch
block of a try-catch
construct, which enables
you to spread the handling of an exception across multiple catch blocks. Avoid using empty
throw statements in scopes that might not contain an exception.
Polyspace Implementation
Polyspace® flags an empty throw statement if it is not within a catch block.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Exception Handling |
Category: Required |
Version History
Introduced in R2024b