18 Language support library [language.support]

18.8 Exception handling [support.exception]

18.8.7 nested_exception [except.nested]

namespace std {
  class nested_exception {
  public:
    nested_exception() noexcept;
    nested_exception(const nested_exception&) noexcept = default;
    nested_exception& operator=(const nested_exception&) noexcept = default;
    virtual ~nested_exception() = default;

    // access functions
    [[noreturn]] void rethrow_nested() const;
    exception_ptr nested_ptr() const noexcept;
  };

  template<class T> [[noreturn]] void throw_with_nested(T&& t);
  template <class E> void rethrow_if_nested(const E& e);
}

The class nested_exception is designed for use as a mixin through multiple inheritance. It captures the currently handled exception and stores it for later use.

Note: nested_exception has a virtual destructor to make it a polymorphic class. Its presence can be tested for with dynamic_cast.  — end note ]

nested_exception() noexcept;

Effects: The constructor calls current_exception() and stores the returned value.

[[noreturn]] void rethrow_nested() const;

Effects: If nested_ptr() returns a null pointer, the function calls std::terminate(). Otherwise, it throws the stored exception captured by *this.

exception_ptr nested_ptr() const noexcept;

Returns: The stored exception captured by this nested_exception object.

template <class T> [[noreturn]] void throw_with_nested(T&& t);

Let U be remove_reference_t<T>.

Requires: U shall be CopyConstructible.

Throws: if is_class_v<U> && !is_final_v<U> && !is_base_of_v<nested_exception, U> is true, an exception of unspecified type that is publicly derived from both U and nested_exception and constructed from std::forward<T>(t), otherwise std::forward<T>(t).

template <class E> void rethrow_if_nested(const E& e);

Effects: If E is not a polymorphic class type, there is no effect. Otherwise, if the static type or the dynamic type of e is nested_exception or is publicly and unambiguously derived from nested_exception, calls:

dynamic_cast<const nested_exception&>(e).rethrow_nested();