libstdc++
nested_exception.h
Go to the documentation of this file.
1 // Nested Exception support header (nested_exception class) for -*- C++ -*-
2 
3 // Copyright (C) 2009-2026 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/nested_exception.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{exception}
28  */
29 
30 #ifndef _GLIBCXX_NESTED_EXCEPTION_H
31 #define _GLIBCXX_NESTED_EXCEPTION_H 1
32 
33 #if __cplusplus < 201103L
34 # include <bits/c++0x_warning.h>
35 #else
36 
37 #include <bits/move.h>
38 #include <bits/exception_ptr.h>
39 
40 extern "C++" {
41 
42 namespace std _GLIBCXX_VISIBILITY(default)
43 {
44  /**
45  * @addtogroup exceptions
46  * @{
47  */
48 
49  /** Mixin class that stores the current exception.
50  *
51  * This type can be used via `std::throw_with_nested` to store
52  * the current exception nested within another exception.
53  *
54  * @headerfile exception
55  * @since C++11
56  * @see std::throw_with_nested
57  * @ingroup exceptions
58  */
60  {
61  exception_ptr _M_ptr;
62 
63  public:
64  /// The default constructor stores the current exception (if any).
65  _GLIBCXX26_CONSTEXPR
66  nested_exception() noexcept : _M_ptr(current_exception()) { }
67 
68  _GLIBCXX26_CONSTEXPR
69  nested_exception(const nested_exception&) noexcept = default;
70 
71  _GLIBCXX26_CONSTEXPR
72  nested_exception& operator=(const nested_exception&) noexcept = default;
73 
74 #if __cplusplus >= 202400L
75  [[__gnu__::__gnu_inline__]]
76  constexpr inline virtual ~nested_exception() noexcept {}
77 #else
78  virtual ~nested_exception() noexcept;
79 #endif
80 
81  /// Rethrow the stored exception, or terminate if none was stored.
82  [[noreturn]]
83  _GLIBCXX26_CONSTEXPR void
85  {
86  if (_M_ptr)
87  rethrow_exception(_M_ptr);
89  }
90 
91  /// Access the stored exception.
92  _GLIBCXX26_CONSTEXPR exception_ptr
93  nested_ptr() const noexcept
94  { return _M_ptr; }
95  };
96 
97  /// @cond undocumented
98 
99  template<typename _Except>
100  struct _Nested_exception : public _Except, public nested_exception
101  {
102  _GLIBCXX26_CONSTEXPR explicit _Nested_exception(const _Except& __ex)
103  : _Except(__ex)
104  { }
105 
106  _GLIBCXX26_CONSTEXPR explicit _Nested_exception(_Except&& __ex)
107  : _Except(static_cast<_Except&&>(__ex))
108  { }
109  };
110 
111 #if __cplusplus < 201703L || ! defined __cpp_if_constexpr
112  // [except.nested]/8
113  // Throw an exception of unspecified type that is publicly derived from
114  // both remove_reference_t<_Tp> and nested_exception.
115  template<typename _Tp>
116  [[noreturn]]
117  inline void
118  __throw_with_nested_impl(_Tp&& __t, true_type)
119  {
120  throw _Nested_exception<__remove_cvref_t<_Tp>>{std::forward<_Tp>(__t)};
121  }
122 
123  template<typename _Tp>
124  [[noreturn]]
125  inline void
126  __throw_with_nested_impl(_Tp&& __t, false_type)
127  { throw std::forward<_Tp>(__t); }
128 #endif
129 
130  /// @endcond
131 
132  /** Throw an exception that also stores the currently active exception.
133  *
134  * If `_Tp` is derived from `std::nested_exception` or is not usable
135  * as a base-class, throws a copy of `__t`.
136  * Otherwise, throws an object of an implementation-defined type derived
137  * from both `_Tp` and `std::nested_exception`, containing a copy of `__t`
138  * and the result of `std::current_exception()`.
139  *
140  * In other words, throws the argument as a new exception that contains
141  * the currently active exception nested within it. This is intended for
142  * use in a catch handler to replace the caught exception with a different
143  * type, while still preserving the original exception. When the new
144  * exception is caught, the nested exception can be rethrown by using
145  * `std::rethrow_if_nested`.
146  *
147  * This can be used at API boundaries, for example to catch a library's
148  * internal exception type and rethrow it nested with a `std::runtime_error`,
149  * or vice versa.
150  *
151  * @since C++11
152  */
153  template<typename _Tp>
154  [[noreturn]]
155  _GLIBCXX26_CONSTEXPR inline void
156  throw_with_nested(_Tp&& __t)
157  {
158  using _Up = typename decay<_Tp>::type;
159  using _CopyConstructible
160  = __and_<is_copy_constructible<_Up>, is_move_constructible<_Up>>;
161  static_assert(_CopyConstructible::value,
162  "throw_with_nested argument must be CopyConstructible");
163 
164 #if __cplusplus >= 201703L && __cpp_if_constexpr
165  if constexpr (is_class_v<_Up>)
166  if constexpr (!is_final_v<_Up>)
167  if constexpr (!is_base_of_v<nested_exception, _Up>)
168  throw _Nested_exception<_Up>{std::forward<_Tp>(__t)};
169  throw std::forward<_Tp>(__t);
170 #else
171  using __nest = __and_<is_class<_Up>, __bool_constant<!__is_final(_Up)>,
172  __not_<is_base_of<nested_exception, _Up>>>;
173  std::__throw_with_nested_impl(std::forward<_Tp>(__t), __nest{});
174 #endif
175  }
176 
177 #if __cplusplus < 201703L || ! defined __cpp_if_constexpr
178  /// @cond undocumented
179 
180  // Attempt dynamic_cast to nested_exception and call rethrow_nested().
181  template<typename _Ex>
182  inline void
183  __rethrow_if_nested_impl(const _Ex* __ptr, true_type)
184  {
185  if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
186  __ne_ptr->rethrow_nested();
187  }
188 
189  // Otherwise, no effects.
190  inline void
191  __rethrow_if_nested_impl(const void*, false_type)
192  { }
193 
194  /// @endcond
195 #endif
196 
197  /** Rethrow a nested exception
198  *
199  * If `__ex` contains a `std::nested_exception` object, call its
200  * `rethrow_nested()` member to rethrow the stored exception.
201  *
202  * After catching an exception thrown by a call to `std::throw_with_nested`
203  * this function can be used to rethrow the exception that was active when
204  * `std::throw_with_nested` was called.
205  *
206  * @since C++11
207  */
208  // _GLIBCXX_RESOLVE_LIB_DEFECTS
209  // 2484. rethrow_if_nested() is doubly unimplementable
210  // 2784. Resolution to LWG 2484 is missing "otherwise, no effects" and [...]
211  template<typename _Ex>
212 # if ! __cpp_rtti
213  [[__gnu__::__always_inline__]]
214 #endif
215  _GLIBCXX26_CONSTEXPR inline void
216  rethrow_if_nested(const _Ex& __ex)
217  {
218  const _Ex* __ptr = __builtin_addressof(__ex);
219 #if __cplusplus < 201703L || ! defined __cpp_if_constexpr
220 # if __cpp_rtti
221  using __cast = __and_<is_polymorphic<_Ex>,
222  __or_<__not_<is_base_of<nested_exception, _Ex>>,
223  is_convertible<_Ex*, nested_exception*>>>;
224 # else
225  using __cast = __and_<is_polymorphic<_Ex>,
227  is_convertible<_Ex*, nested_exception*>>;
228 # endif
229  std::__rethrow_if_nested_impl(__ptr, __cast{});
230 #else
231  if constexpr (!is_polymorphic_v<_Ex>)
232  return;
233  else if constexpr (is_base_of_v<nested_exception, _Ex>
234  && !is_convertible_v<_Ex*, nested_exception*>)
235  return; // nested_exception base class is inaccessible or ambiguous.
236 # if ! __cpp_rtti
237  else if constexpr (!is_base_of_v<nested_exception, _Ex>)
238  return; // Cannot do polymorphic casts without RTTI.
239 # endif
240  else if (auto __ne_ptr = dynamic_cast<const nested_exception*>(__ptr))
241  __ne_ptr->rethrow_nested();
242 #endif
243  }
244 
245  /// @} group exceptions
246 } // namespace std
247 
248 } // extern "C++"
249 
250 #endif // C++11
251 #endif // _GLIBCXX_NESTED_EXCEPTION_H
_GLIBCXX26_CONSTEXPR nested_exception() noexcept
The default constructor stores the current exception (if any).
exception_ptr current_exception() noexcept
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:119
void rethrow_exception(exception_ptr)
Throw the object pointed to by the exception_ptr.
_GLIBCXX26_CONSTEXPR void rethrow_nested() const
Rethrow the stored exception, or terminate if none was stored.
ISO C++ entities toplevel namespace is std.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:122
_GLIBCXX26_CONSTEXPR void throw_with_nested(_Tp &&__t)
_GLIBCXX26_CONSTEXPR void rethrow_if_nested(const _Ex &__ex)
is_base_of
Definition: type_traits:1638
is_move_constructible
Definition: type_traits:1300
void terminate() noexcept
An opaque pointer to an arbitrary exception.
_GLIBCXX26_CONSTEXPR exception_ptr nested_ptr() const noexcept
Access the stored exception.