AUTOSAR C++14 Rule A15-1-3
Description
Rule Definition
All thrown exceptions should be unique.
Rationale
If the same object is raised as exceptions in multiple places, handling these exceptions and debugging the code can be difficult. Raising unique exception objects simplifies the debugging process. Consider this code where multiple exceptions are raised.
void f1(){ //... throw std::logic_error("Error"); } void f2(){ //... throw std::logic_error("Error"); } void f3(){ //... throw std::logic_error("f3: Unexpected Condition"); } int main(){ try{ f1(); f2(); f3(); catch(std::logic_error& e){ std::cout << e.what() << '\n'; } } }
f1()
and f2()
raise the same exception,
while f3()
raises a unique exception. During debugging, you cannot
determine if an exception arises from f1()
or f2()
.
You know when an exception arises from f3()
. To make the debugging
process simpler, raise unique exceptions. An exception is unique if either of these
conditions is true:
The exception type does not occur elsewhere in your project.
The error message or the error code does not occur elsewhere in your project.
Polyspace Implementation
Polyspace® highlights throw
statements that raise the same class, enum
value, integer, or constant literal as exceptions, and flags the final throw statement that
raise the same object. You might want to raise the same exception in multiple places by
using a preconstructed exception object. Polyspace does not flag throw
statements that raise such
preconstructed exception objects. If you raise the same literal object in multiple places,
Polyspace does not flag it if the literal is not a constant or if the literal is hidden
behind a variable.
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: Advisory, Automated |
Version History
Introduced in R2020b