AutosarC++17_03-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
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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 270728 2017-03-16 10:38:20Z piotr.tanski $
#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.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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_implicit_noexcept_on_destructors¶
allow_implicit_noexcept_on_destructors : bool = False
exclude_exception_base_classes¶
exclude_exception_base_classes : set[bauhaus.analysis.config.QualifiedName] = set()
generate_violation_path¶
generate_violation_path : bool = True