Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The C++ Standard allows the copy constructor to be elided when initializing the exception object to perform the initialization if a temporary is thrown. Many modern compiler implementations make use of both optimization techniques. However, the copy constructor for an exception object still must not throw an exception because compilers are not required to elide the copy constructor call in all situations, and common implementations of std::exception_ptr will call a copy constructor even if it can be elided from a throw expression.

...

In this noncompliant code example, an exception of type S is thrown from f(). However, because S has a std::string data member, and the copy constructor for std::string is not declared noexcept, the implicitly - defined copy constructor for S is also not declared to be noexcept. In low-memory situations, the copy constructor for std::string may be unable to allocate sufficient memory to complete the copy operation, resulting in a std::bad_alloc exception being thrown.

...

If the exception type cannot be modified to inherit from std::runtime_error, a data member of that type is an unexpected , but legitimate implementation strategy, as shown in this compliant solution:

...

Bibliography

[Hinnant 2015] 
[ISO/IEC 14882-2014]

Subclause 15.1, "Throwing an Exception"
Subclause 18.8.5, "Exception Propagation"

[Hinnant 2015]

 

 

...

Image Modified  Image Modified