Throwing Non-Exceptions with std::throw_with_nested()
What happens if I pass a non-exception type to std::throw_with_nested()?
If you pass a non-exception type to std::throw_with_nested(), the behavior is undefined. The C++ standard requires that the argument to std::throw_with_nested() be derived from std::exception.
For example, this code has undefined behavior:
try {
std::throw_with_nested(42);
} catch(...) {
// undefined behavior
}To properly use std::throw_with_nested(), ensure you only pass it exceptions:
try {
std::throw_with_nested(
std::runtime_error{"Error"});
} catch (...) {
// OK, runtime_error derives from std::exception
}Nested Exceptions
Learn about nested exceptions in C++: from basic concepts to advanced handling techniques