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 <typename E>
std::enable_if_t<!std::is_polymorphic<E>::value>
my_rethrow_if_nested(const E&) {}
template <typename E>
std::enable_if_t<std::is_polymorphic<E>::value>
my_rethrow_if_nested(const E& e)
{
const auto* p = dynamic_cast<const std::nested_exception*>(std::addressof(e));
if (p && p->nested_ptr()) {
p->rethrow_nested();
}
}
Demo