Throwing an exception from a destructor results in undefined behavior, meaning that your program could be terminated abruptly without neatly destroying others objects.


Thus destructors should never ``++throw++`` exceptions. Instead, they should catch and handle those thrown by the functions they call, and be ``++noexcept++``.


This rule raises an issue when a destructor is not ``++noexcept++``. By default, destructors are ``++noexcept++``, therefore most of the time, nothing needs to be written in the source code. A destructor is not ``++noexcept++`` if:

* the base class or a data member has a non ``++noexcept++`` destructor,
* the destructor is decorated with the ``++noexcept++`` keyword followed by something that evaluates to false.


== Noncompliant Code Example

----
struct A {
  ~A() noexcept(false) {} // Noncompliant
};

struct C {
  /* ... */
  A a; // This member data prevents automatic declaration of the destructor as noexcept
  ~C() { // Noncompliant
    /* ... */
  }
};
----


== Compliant Solution

----
struct A {
  ~A() noexcept(true) {}
};

struct C {
  /* ... */
  A a;
  ~C() { // Compliant, noexcept by default
    /* ... */
  }
};
----


== See

* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#c36-a-destructor-may-not-fail[{cpp} Core Guidelines C.36] - A destructor may not fail
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#c37-make-destructors-noexcept[{cpp} Core Guidelines C.37] - Make destructors noexcept


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
