The C++ Standard, [except.handle], paragraph 4 [ISO/IEC 14882-2014], states the following:
The handlers for a try block are tried in order of appearance. That makes it possible to write handlers that can never be executed, for example by placing a handler for a derived class after a handler for a corresponding base class.
Consequently, if two handlers catch exceptions that are derived from the same base class (such as std::exception
), the most derived exception must come first.
...
Exception handlers with inverted priorities cause unexpected control flow when an exception of the derived type occurs.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR54-CPP | Medium | Likely | Low | P18 | L1 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| exception-caught-by-earlier-handler | Fully checked | ||||||
Axivion Bauhaus Suite |
| CertC++-ERR54 | |||||||
Clang |
| -Wexceptions |
CodeSonar |
| LANG.STRUCT.UCTCH | Unreachable Catch | ||||||
| CP1.ERR36 | Fully implemented | |||||||
Helix QAC |
| C++4030, C++4639 | |||||||
Klocwork |
| MISRA.CATCH.NOALL MISRA.CATCH.WRONGORD | |||||||
LDRA tool suite |
| 541 S, 556 S | Fully implemented | ||||||
Parasoft C/C++test |
| CERT_CPP-ERR54-a | Where multiple handlers are provided in a single try-catch statement or function-try-block for a derived class and some or all of its bases, the handlers shall be ordered most-derived to base class | |||||||
Polyspace Bug Finder |
| CERT C++: ERR54-CPP | Checks for:
Rule fully covered. | ||||||
PVS-Studio |
| V759 | |||||||
RuleChecker |
| exception-caught-by-earlier-handler | Fully checked | ||||||
SonarQube C/C++ Plugin |
| S1045 |
Related Vulnerabilities
Search for other vulnerabilities resulting resulting from the violation of this rule on the CERT website.
Related Guidelines
[MISRA 08] | Rule 15-3-6 (Required) |
Bibliography
[ISO/IEC 14882-2014] | Subclause 15.3, "Handling an Exception" |
...
...