Skip to main content

IAR Embedded Workbench for Arm 9.70.x

THROW-static (C++ only)

In this section:
Synopsis

Exceptions thrown without a handler in some call paths that lead to that point.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

There are exceptions thrown without a handler in some call paths that lead to that point. If an application throws an unhandled exception, it terminates in an implementation-defined manner. In particular, it is implementation-defined whether the call stack is unwound before termination, so the destructors of any automatic objects might not be invoked. If an exception is thrown as an object of a derived class, a compatible type might be either the derived class or any of its bases. Make sure that the application catches all exceptions it is expected to throw. This check is identical to MISRAC++2008-15-3-1.

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

(Required) Exceptions shall be raised only after start-up and before termination of the program.

Code examples

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

class C {
public: 
  C ( ) { throw ( 0 ); } // Non-compliant – thrown before main starts
  ~C ( ) { throw ( 0 ); } // Non-compliant – thrown after main exits
};

// An exception thrown in C's constructor or destructor will
// cause the program to terminate, and will not be caught by
// the handler in main
C c;  

int main( ... )
{
  try {
    // program code
    return 0;
  }
  // The following catch-all exception handler can only
  // catch exceptions thrown in the above program code
  catch ( ... ) {
    // Handle exception
    return 0;
  }
}

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

class C {
public:
    C ( ) {  }  // Compliant – doesn't throw exceptions
    ~C ( ) {  } // Compliant – doesn't throw exceptions
};

C c;

int main( ... )
{
    try {
        // program code
        return 0;
    }
    // The following catch-all exception handler can only
    // catch exceptions thrown in the above program code
    catch ( ... ) {
        // Handle exception
        return 0;
    }
}