AutosarC++17_03-A15.2.1¶
Constructors that are not noexcept shall not be invoked before program startup
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Before the program starts executing the body of main function, it is in a start-up phase, constructing and initializing static objects. There is nowhere an exception handler can be placed to catch exceptions thrown during this phase, so if an exception is thrown it leads to the program being terminated in an implementation-defined manner.
Such errors may be more difficult to find because an error message can not be logged, due to lack of exception handling mechanism during static initialization.
Example
//% $Id: A15-2-1.cpp 271927 2017-03-24 12:01:35Z piotr.tanski $
#include <cstdint>
#include <stdexcept>
class A {
public:
A() noexcept : x(0) {
// ...
}
explicit A(std::int32_t n) : x(n) {
// ...
throw std::runtime_error("Unexpected error");
}
A(std::int32_t i, std::int32_t j) noexcept : x(i + j) {
try {
// ...
throw std::runtime_error("Error");
// ...
}
catch (std::exception &e) {
}
}
private:
std::int32_t x;
};
static A a1; // Compliant - default constructor of type A is noexcept
static A a2(5); // Non-compliant - constructor of type A throws, and the
// exception will not be caught by the handler in main function
static A a3(5, 10); // Compliant - constructor of type A is noexcept, it
// handles exceptions internally
int main(int, char **) {
try {
// program code
} catch (...) {
// Handle exceptions
}
return 0;
}
See Also
- SEI CERT C++ [9]: ERR51-CPP. Handle all exceptions.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
static_field_only_noexcept |
Constructor called that may throw an exception in static field. |
None |
False |
static_variable_only_noexcept |
Constructor called that may throw an exception in static variable. |
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
ignore_const¶
ignore_const : bool = False