Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.DTOR.THROW

Throw in destructor.

MISRA-C++ Rule 15-5-1 (required): A class destructor shall not exit with an exception.

[Implementation 15.3(9), 15.5.1(2)]

Rationale

When an exception is thrown, the call stack is unwound up to the point where the exception is to be handled. The destructors for all automatic objects declared between the point where the exception is thrown and where it is to be handled will be invoked. If one of these destructors exits with an exception, then the program will terminate in an implementation-defined manner.

Note that it is acceptable for a destructor to throw an exception that is handled within the destructor, for example within a try-catch block.

Example

class C1
{
public:
   ~C1 ( )
   {
      try
      {
         throw ( 42 );    // Compliant - exception will not leave
                          // destructor
      }

      catch ( int32_t i ) // int handler
      {
         // Handle int exception throw by destructor
      }
   }
};

class C2
{
public:
   ~C2 ( )
   {
      throw ( 42 );   // Non-compliant - destructor exits with an
                      // exception
   }
};

void foo ( )
{
   C2 c; // program terminates when c is destroyed

   throw ( 10 );
}