AUTOSAR C++14 Rule M15-3-4
Each exception explicitly thrown in the code shall have a handler of a compatible type in all call paths that could lead to that point
Since R2020b
Description
Rule Definition
Each exception explicitly thrown in the code shall have a handler of a compatible type in all call paths that could lead to that point.
Rationale
In C++, when an operation raises an exception, the compiler tries to match the exception
with a compatible exception handler in the current and adjacent scopes. If no compatible
exception handler for a raised exception exists, the compiler invokes the function
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 particular set of software and hardware that you use. For instance,
std::terminate()
might invoke std::abort()
to
abnormally abort the execution without unwinding the stack. If the stack is not unwound
before program termination, then the destructors of the variables in the stack are not
invoked, leading to resource leak and security vulnerabilities.
Consider this code where multiple exceptions are raised in the try block of code.
class General{/*... */}; class Specific : public General{/*...*/}; class Different{} void foo() noexcept { try{ //... throw(General e); //.. throw( Specific e); // ... throw(Different e); } catch (General& b){ } }
General
. This
catch block is compatible with exceptions of the base class General
and
the derived class Specific
. The exception of class
Different
does not have a compatible handler. This unhandled exception
violates this rule and might result in resource leaks and security vulnerabilities.Because unhandled exceptions can lead to resource leak and security vulnerabilities, match the explicitly raised exceptions in your code with a compatible handler.
Polyspace Implementation
Polyspace® flags a
throw
statement in a function if a compatible catch statement is absent in the call path of the function. If the function is not specified asnoexcept
, Polyspace ignores it if its call path lacks an entry point likemain()
.Polyspace flags a
throw
statement that uses acatch(…)
statement to handle the raised exceptions.Polyspace does not flag rethrow statements, that is,
throw
statements within catch blocks.You might have compatible catch blocks for the
throw
statements in your function in a nested try-catch block Polyspace ignores nested try-catch blocks. Justifythrow
statements that have compatible catch blocks in a nested structure by using comments. Alternatively, use a single level of try-catch in your functions.
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, Automated |
Version History
Introduced in R2020b