AutosarC++17_10-A15.5.1

All user-provided class destructors, deallocation functions, move constructors, move assignment operators and swap functions shall not exit with an exception. A noexcept exception specification shall be added to these functions as appropriate

Required inputs: IR, StaticSemanticAnalysis

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 or “delete” operators exits with an exception, then the program will terminate in an implementation-defined manner.

Move constructor and move assignment operator are intended to be noexcept. If they throw exceptions, strong exception safety can not be guaranteed, because the original type values could be already modified or partially modified.

The standard-library containers and algorithms will not work correctly if a swap of two elements exits with an exception.

Note that if move constructor is not noexcept, then the standard library containers will use the copy constructor rather than the move constructor.

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

Also, note that a destructor is noexcept by default, but the keyword noexcept needs to be specified to explicitly state that it can not throw any exception.

Example
//% $Id: A15-5-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <stdexcept>
class C1 {
public:
  C1() = default;
  C1(C1 &&rhs)
  noexcept // Compliant - move constructor declared to be noexcept
  {}
  C1 &operator=(C1 &&rhs) noexcept // Compliant - move assignment operator
                                   // declared to be noexcept
  {
    return *this;
  }
  ~C1() noexcept // Compliant - no exceptions thrown from destructor
  {}
};
void Swap(C1 &lhs, C1 &rhs) noexcept // Compliant - swap function does not exit
                                     // with an exception
{
  // Implementation
}
class C2 {
public:
  C2() = default;
  C2(C2 &&rhs)
  noexcept // Compliant - move constructor declared to be noexcept
  {
    try {
      // ...
      throw std::runtime_error(
          "Error"); // Exception will not leave move constructor
    }

    catch (std::exception &e) {
      // Handle runtime error
    }
  }
  C2 &operator=(C2 &&rhs) noexcept // Compliant - move assignment operator
                                   // declared to be noexcept
  {
    try {
      // ...
      throw std::runtime_error(
          "Error"); // Exception will not leave assignment operator
    }

    catch (std::exception &e) {
      // Handle runtime error
    }

    return *this;
  }
  ~C2() // Non-compliant - the destructor does not contain the noexcept
        // specification
  {
    try {
      // ...
      throw std::runtime_error(
          "Error"); // Exception will not leave the destructor
    }

    catch (std::exception &e) {
      // Handle runtime error
    }
  }
};
void Swap(C2 &lhs, C2 &rhs) noexcept(
    false) // Non-compliant - swap function declared to be noexcept(false)
{
  // Implementation
  throw std::runtime_error("Swap function failed"); // Non-compliant - swap
                                                    // function exits with an
                                                    // exception
}
class C3 {
public:
  C3() = default;
  C3(C3 &&rhs) // Non-compliant - move constructor throws
  {
    // ...
    throw std::runtime_error("Error");
  }
  C3 &operator=(C3 &&rhs) // Non-compliant - move assignment operator throws
  {
    // ...
    throw std::runtime_error("Error");
    return *this;
  }
  ~C3() // Non-compliant - destructor exits with an exception
  {
    throw std::runtime_error("Error");
  }
  static void operator delete(void *ptr, std::size_t sz) {
    // ...
    throw std::runtime_error("Error"); // Non-compliant - operator delete
                                       // exits with an exception
  }
};
void Fn() {
  C3 c1; // program terminates when c1 is destroyed
  C3 *c2 = new C3;
  // ...
  delete c2; // program terminates when c2 is deleted
}
See Also
  • MISRA C++ 2008 [6]: 15-5-1 A class destructor shall not exit with an exception.
  • HIC++ v4.0 [8]: 15.2.1 Do not throw an exception from a destructor
  • C++ Core Guidelines [10]: E.16: Destructors, deallocation, and swap must never fail
  • C++ Core Guidelines [10]: C.85: Make swap noexcept

Possible Messages

Key

Text

Severity

Disabled

routine_may_except

Function must not exit with an exception.

None

False

routine_not_noexcept

Function shall be explicitly declared noexcept if appropriate.

None

False

Options

allow_implicit_noexcept_on_destructors

allow_implicit_noexcept_on_destructors : bool = False

Whether to omit a violation on destructors if no explicit noexcept specification exists. The Autosar C++ guidelines releases until/including 17-10 required an explicit noexcept. Since the Autosar C++ guidelines release 18-03 one may include an explicit noexcept specification, but it is clear from the associated example that it is no longer required.
 

exclude_exception_base_classes

exclude_exception_base_classes : set[bauhaus.analysis.config.QualifiedName] = set()

Exclude issues for the exception types mentioned in this set of qualified names. Also excludes classes derived from those class names as well as pointers or references to any of these class types.
 

generate_violation_path

generate_violation_path : bool = True

Whether to compute a trace for the exception. This improves the usability of the violation description, but requires additional computing which might slow down the rule.