...
When declaring an object with static or thread storage duration, and that object is not declared within a function block scope, the type's constructor must be declared noexcept(true)
and must comply with ERR55-CPP. Honor exception specifications. If an exception escaped a constructor that was not declared noexcept(true)
, the behavior is implementation-defined.
...
In this noncompliant example, the constructor of global
may throw an exception during program startup (the std::string
constructor accepting a const char *
and a default allocator object is not marked noexcept(true)
and consequently allows all exceptions). This exception is not caught by the function-try-block on main()
, resulting in a call to std::terminate()
and abnormal program termination.
...
This compliant solution introduces a class derived from std::string
with a constructor that catches all exceptions with a function try block and terminates the application in accordance with ERR50-CPP. Do not abruptly terminate the program in the event any exceptions are thrown. Because no exceptions can escape the constructor, it is marked noexcept(true)
and is permissible to use as a static global variable.
...