Main Content

AUTOSAR C++14 Rule M15-3-7

Where multiple handlers are provided in a single try-catch statement or function-try-block, any ellipsis (catch-all) handler shall occur last

Description

Rule Definition

Where multiple handlers are provided in a single try-catch statement or function-try-block, any ellipsis (catch-all) handler shall occur last.

Rationale

In a try-catch statement or function-try block, the compiler matches the raised exception with a catch() handler. The catch(…) handler matches any exception. Handlers after the catch-all handler within the same try-catch statement or function try-block are ignored by the compiler during the exception handling process and are unreachable code.

Having a handler after the catch-all handler might result in developer confusion as to why certain intended handlers are not being executed. Likewise, the catch-all handler might not handle the exception in the way the developer intends, resulting in confusion.

Polyspace Implementation

Polyspace® raises this defect whenever a handler appears after the catch-all handler within the try-catch statement or function try-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

expand all

#include <iostream>
#include <exception>

using namespace std;

int main() {

    try {
        // some code
    } catch (exception& e1) { // Compliant

        //...

    } catch (...) { // Compliant

        //...

    } catch (exception& e2) { // Noncompliant

        //...
    }

    return 0;
}

Because the catch (exception& e2) handler comes after the catch(…) handler, Polyspace flags the handler before the catch-all handler as noncompliant. This issue might cause a compilation error.

Check Information

Group: Exception Handling
Category: Required, Automated

Version History

Introduced in R2019a