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
}
Answers to questions are automatically generated and may not have been reviewed.
Learn about nested exceptions in C++: from basic concepts to advanced handling techniques