Skip to main content

IAR Embedded Workbench for Arm 9.70.x

CATCH-object-slicing (C++ only)

In this section:
Synopsis

Exception objects are caught by value

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

Class type exception objects are caught by value, leading to slicing. That is, if the exception object is of a derived class and is caught as the base, only the base class's functions (including virtual functions) can be called. Moreover, any additional member data in the derived class cannot be accessed. If the exception is instead caught by reference, slicing does not occur. This check is identical to MISRAC++2008-15-3-5, MISRAC++2023-18.3.2.

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.

MISRA C++ 2023 18.3.2

(Required) An exception of class type shall be caught by const reference or 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
    }
}