AutosarC++17_03-A15.4.2¶
If a function is declared to be noexcept, noexcept(true) or noexcept(<true condition>), then it shall not exit with an exception
Required inputs: IR, StaticSemanticAnalysis
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
If a function declared to be noexcept, noexcept(true) or noexcept(true condition) throws an exception, then std::terminate() is called immediately. It is implementation-defined whether the call stack is unwound before std::terminate() is called.
To ensure that the rule is not violated, if function’s noexcept specification can not be determined, then always declare it to be noexcept(false).
Example
//% $Id: A15-4-2.cpp 271752 2017-03-23 12:07:07Z piotr.tanski $
#include <stdexcept>
// library.h
void libraryFunc();
// project.cpp
void f1() noexcept {
// ...
throw std::runtime_error("Error"); // Non-compliant - f1 declared to be
// noexcept, but exits with exception.
// This leads to std::terminate() call
}
void f2() noexcept(true) {
try {
// ...
throw std::runtime_error(
"Error"); // Compliant - exception will not leave f2
} catch (std::runtime_error &e) {
// Handle runtime error
}
}
void f3() noexcept(false) {
// ...
throw std::runtime_error("Error"); // Compliant
}
void f4() noexcept(
false) // Compliant - no information whether library_func() throws or not
{
libraryFunc();
}
See Also
- MISRA C++ 2008 [6]: 15-5-3 The terminate() function shall not be called implicitly.
- HIC++ v4.0 [8]: 15.3.2 Ensure that a program does not result in a call to std::terminate
- SEI CERT C++ [9]: ERR50-CPP. Do not abruptly terminate the program
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
implicit_noexcept_spec_violation_without |
Function implicitly declared noexcept(false) but no exceptions will be thrown. |
None |
False |
noexcept_spec_violation_with |
Exception violates function’s noexcept-specification. |
None |
False |
noexcept_spec_violation_without |
Function declared noexcept(false) but no exceptions will be thrown. |
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
exclude_exception_base_classes¶
exclude_exception_base_classes : set[bauhaus.analysis.config.QualifiedName] = set()
generate_violation_path¶
generate_violation_path : bool = True
ignore_constructor_destructor¶
ignore_constructor_destructor : bool = False
ignore_implicit_noexcept_spec_lambda¶
ignore_implicit_noexcept_spec_lambda : bool = False
ignore_throwing_functions¶
ignore_throwing_functions : bool = False
ignore_unknown_routines¶
ignore_unknown_routines : bool = False
report_noexcept_false_violations¶
report_noexcept_false_violations : bool = False