Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The C++ 2004, section 15.3 "Handling an Exception", saysStandard, [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 that can never be executed, for example by placing a handler for a derived class after a handler for a corresponding base corresponding base class.

A ... in a handlers exception-declaration functions similarly to ... in a function parameter declaration; it specifies a match for any exception. If present, a ... handler shall be the last handler for its try block.

Consequently Consequently, if two handlers catch exceptions that are derived from the same base class (such as std::exception), the most derived exception should must come first.

...

Noncompliant Code Example

In this non-compliant noncompliant code example, the first handler will catch catches all exceptions of class B, as well as exceptions of class D, since they are also of class B. Consequently, the second handler will does not catch any exceptions.

Code Block
bgColor#FFcccc
langcpp

// classesClasses used for exception handling
class B {};
class D : public B {};

// ... Using the classes from above
void f() {
  try {
    // ...
  } catch (B &b) {
    // ...
  } catch (D &d) {
    // ...
  }
}

Compliant Solution

In this compliant solution, the first handler will catch catches all exceptions of class D, and the second handler will catch catches all the other exceptions of class B.

Code Block
bgColor#ccccff
langcpp#FFcccc

// classesClasses used for exception handling
class B {};
class D : public B {};

// ... Using the classes from above
void f() {
  try {
    // ...
  } catch (D &d) {
    // ...
  } catch (B &b) {
    // ...
  }
}

Risk Assessment

Badly ordering exception handlers can Exception handlers with inverted priorities cause unexpected control flow when an exception of the derived type occurs.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ERR36

ERR54-CPP

3 (high)

3 (likely)

1 (low)

P9

L1

References

Wiki Markup
\[[ISO/IEC 14882-2003|AA. C++ References#ISO/IEC 14882-2003]\]
\[[MISRA 08|AA. C++ References#MISRA 08]\] Rule 15-3-6 & 15-3-7

Medium

Likely

Low

P18

L1

Automated Detection

Tool

Version

Checker

Description

Astrée

Include Page
Astrée_V
Astrée_V

exception-caught-by-earlier-handler
Fully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC++-ERR54
Clang
Include Page
Clang_V
Clang_V
-Wexceptions
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.STRUCT.UCTCH

Unreachable Catch

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CP1.ERR36

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C++4030, C++4639
Klocwork
Include Page
Klocwork_V
Klocwork_V
MISRA.CATCH.NOALL
MISRA.CATCH.WRONGORD 

LDRA tool suite
Include Page
LDRA_V
LDRA_V

541 S, 556 S

Fully implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V
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

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C++: ERR54-CPP

Checks for:

  • Exception handlers not ordered from most-derived to base class
  • Incorrect order of ellipsis handler

Rule fully covered.

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V759
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
exception-caught-by-earlier-handler
Fully checked
SonarQube C/C++ Plugin
Include Page
SonarQube C/C++ Plugin_V
SonarQube C/C++ Plugin_V
S1045

Related Vulnerabilities

Search for other vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

[MISRA 08]

Rule 15-3-6 (Required)
Rule 15-3-7 (Required)

Bibliography

[ISO/IEC 14882-2014]Subclause 15.3, "Handling an Exception"


...

Image Added Image Added Image AddedERR09-CPP. Throw anonymous temporaries and catch by reference      12. Exceptions and Error Handling (ERR)      ERR31-CPP. Don't redefine errno