Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MISRAC++2023-18.3.2

In this section:
Synopsis

(Required) An exception of class type shall be caught by const reference or reference

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

Exception objects are caught by value, not by reference. This check is identical to CATCH-object-slicing, MISRAC++2008-15-3-5.

Coding standards
CERT ERR09-CPP

Throw anonymous temporaries and catch by reference

MISRA C++ 2008 15-3-5

(Required) A class type exception shall always be caught by reference.

Code examples

The following code example fails the check and will give a warning:

typedef char char_t;

// base class for exceptions
class ExpBase {
public:
    virtual const char_t *who ( ) { return "base"; }
};

class ExpD1: public ExpBase {
public:
    virtual const char_t *who ( ) { return "type 1 exception"; }
};

class ExpD2: public ExpBase {
public:
    virtual const char_t *who ( ) { return "type 2 exception"; }
};

void example()
{
    try {
        // ...
        throw ExpD1 ( );
        // ...
        throw ExpBase ( );
    }
    catch ( ExpBase b ) { // Non-compliant - derived type objects will be
                          // caught as the base type
        b.who();          // Will always be "base"
        throw b;          // The exception re-thrown is of the base class,
                          // not the original exception type
    }
}

The following code example passes the check and will not give a warning about this issue:

typedef char char_t;

// base class for exceptions
class ExpBase {
public:
    virtual const char_t *who ( ) { return "base"; }
};

class ExpD1: public ExpBase {
public:
    virtual const char_t *who ( ) { return "type 1 exception"; }
};

class ExpD2: public ExpBase {
public:
    virtual const char_t *who ( ) { return "type 2 exception"; }
};

void example()
{
    try {
        // ...
        throw ExpD1 ( );
        // ...
        throw ExpBase ( );
    }
    catch ( ExpBase &b ) { // Compliant – exceptions caught by reference
        // ...
        b.who(); // "base", "type 1 exception" or "type 2 exception"
                 // depending upon the type of the thrown object
    }
}