Destructor may throw
Description
This defect occurs if either of these conditions are true:
A destructor signature explicitly permits raising exceptions using the
noexcept(false)specification (C++11 and later) or a nonemptythrowspecification (C++03 and earlier).A destructor signature implicitly permits raising exceptions. If the destructor is implicit or lacks an explicit exception specification, raising exceptions is permitted for C++03 and earlier. For C++11 and later, implicit destructors and destructors lacking an explicit exception specification are assumed to be
noexcept(true).
Risk
Using destructors that can raise exceptions has these risks:
When an exception occurs, the compiler unwinds the stack and deletes objects by calling their destructors. If a destructor raises an exception during stack unwinding, the program terminates abnormally.
Destructors that are permitted to raise exceptions require the compiler to generate exception handling code in the code of the function that calls the destructor. Such code is less efficient than code that calls
noexceptspecified destructors.Parts of the Standard Template Library (STL) executes more expensive code to avoid throwing exceptions. If the exception specification of a destructor permits exceptions, the code incurs avoidable performance overhead and execute less efficiently than intended.
Fix
To fix this defect:
Explicitly specify the destructor as
noexcept,noexcept(true)orthrow().For C++11 or later, use the default destructor or the implicit destructor.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
DESTRUCTOR_MAY_THROW
|
| Impact: Medium |
PQL Name:
std.defects.DESTRUCTOR_MAY_THROW
|
Version History
Introduced in R2026a
See Also
Find defects
(-checkers) | C++ standard version
(-cpp-version) | Swap may
throw | A move operation may
throw | AUTOSAR C++14 Rule
A15-5-1
Topics
- Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Polyspace Results Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)
- Expensive-to-Read Objects in Bug Finder