std::unexpected is the runtime response to an exception that violates a dynamic exception specification. Its default behavior calls terminate, which calls abort, turning an incorrect exception guess into an abrupt crash. section 10.8
Dynamic exception specifications create several practical problems:
- They add runtime checks that match thrown exceptions against the specification and may affect performance. section 10.8
- A conversion function installed with set_unexpected can replace the failure with a known exception, but may erase useful context across the whole program. section 10.8
- Removing the specification avoids behavior that differs from ordinary exception propagation and is the recommended modern correction. section 10.8
A hidden specification failure
A std_dev function declares only std::invalid_argument, but arith_mean can throw std::domain_error for empty input. That undeclared exception triggers std::unexpected instead of propagating naturally. Removing the specification lets both errors remain visible to appropriate handlers.
