3.9 KiB
[except.nested]
17 Language support library [support]
17.9 Exception handling [support.exception]
17.9.8 nested_exception [except.nested]
namespace std {class nested_exception {public:constexpr nested_exception() noexcept; constexpr nested_exception(const nested_exception&) noexcept = default; constexpr nested_exception& operator=(const nested_exception&) noexcept = default; constexpr virtual ~nested_exception() = default; // access functionsnoreturn constexpr void rethrow_nested() const; constexpr exception_ptr nested_ptr() const noexcept; }; template noreturn constexpr void throw_with_nested(T&& t); template constexpr 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 1:
nested_exception has a virtual destructor to make it a polymorphic class.
Its presence can be tested for with dynamic_cast.
â end note]
constexpr nested_exception() noexcept;
Effects: The constructor calls current_exception() and stores the returned value.
[[noreturn]] constexpr void rethrow_nested() const;
Effects: If nested_ptr() returns a null pointer, the function calls the function std::terminate.
Otherwise, it throws the stored exception captured by *this.
constexpr exception_ptr nested_ptr() const noexcept;
Returns: The stored exception captured by this nested_exception object.
template<class T> [[noreturn]] constexpr void throw_with_nested(T&& t);
Let U be decay_t.
Preconditions: U meets the Cpp17CopyConstructible requirements.
Throws: If is_class_v && !is_final_v && !is_base_of_v<nested_exception, U> is true, an exception of unspecified type that is publicly derived from bothU and nested_exception and constructed from std::forward(t), otherwisestd::forward(t).
template<class E> constexpr void rethrow_if_nested(const E& e);
Effects: If E is not a polymorphic class type, or if nested_exception is an inaccessible or ambiguous base class of E, there is no effect.
Otherwise, performs:if (auto p = dynamic_cast<const nested_exception*>(addressof(e))) p->rethrow_nested();