I\'m trying to print nested exceptions using the following example code from cppreference.com:
void print_exception(const std::exception& e, int level = 0)
You may write something like:
// Similar to rethrow_if_nested
// but does nothing instead of calling std::terminate
// when std::nested_exception is nullptr.
template
std::enable_if_t::value>
my_rethrow_if_nested(const E&) {}
template
std::enable_if_t::value>
my_rethrow_if_nested(const E& e)
{
const auto* p = dynamic_cast(std::addressof(e));
if (p && p->nested_ptr()) {
p->rethrow_nested();
}
}
Demo