Skip to main content

IAR Embedded Workbench for RX 5.20

MISRAC++2023-18.3.3

In this section:
Synopsis

(Required) Handlers for a function-try-block of a constructor or destructor shall not refer to non-static members from their class or its bases

Enabled by default

Yes

Severity/Certainty

Medium/Low

mediumlow.png
Full description

One or more exception handlers in a constructor or destructor accesses a non-static member variable that might not exist. This check is identical to CATCH-xtor-bad-member, MISRAC++2008-15-3-3.

Coding standards
MISRA C++ 2008 15-3-3

(Required) Handlers of a function-try-block implementation of a class constructor or destructor shall not reference non-static members from this class or its bases.

Code examples

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

#ifndef __EXCEPTIONS
    #error "IGNORE_TEST: requires exceptions"
#endif

int throws();

class C
{
public:
  int x;
  static char c;
  C ( )
  {
    x = 0;
  }

  ~C ( )
  {
    try
    {
      throws();
      // Action that may raise an exception
    }
    catch ( ... )
    {
      if ( 0 == x ) // Non-compliant – x may not exist at this point
      {
        // Action dependent on value of x
      }
    }
  }
};

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

#ifndef __EXCEPTIONS
    #error "IGNORE_TEST: requires exceptions"
#endif

class C
{
public:
  int x;
  static char c;
  C ( )
  {
    try
    {
      // Action that may raise an exception
    }
    catch ( ... )
    {
      if ( 0 == c )
      {
        // Action dependent on value of c
      }
    }
  }

  ~C ( )
  {
    try
    {
      // Action that may raise an exception
    }
    catch (int i) {}
    catch ( ... )
    {
      if ( 0 == c )
      {
        // Action dependent on value of c
      }
    }
  }
};