COP-dtor-throw (C++ only)
In this section:
Synopsis
An exception is thrown, or might be thrown, in a class destructor.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
An exception is thrown, or might be thrown, in a class destructor. When the destructor is called, stack unwinding takes place. If an exception is thrown at this time, the application will crash. This check is identical to MISRAC++2008-15-5-1.
Coding standards
- CERT ERR33-CPP
Destructors must not throw exceptions
- MISRA C++ 2008 15-5-1
(Required) A class destructor shall not exit with an exception.
Code examples
The following code example fails the check and will give a warning:
class E{};
class C {
~C() {
if (!p){
throw E(); //may throw an exception here
}
}
int* p;
};
The following code example passes the check and will not give a warning about this issue:
void do_something();
class C {
~C() { //OK
if (!p){
do_something();
}
}
int* p;
};