libstdc++
stdexcept_except.h
Go to the documentation of this file.
1 // Exception classes for <stdexcept> -*- C++ -*-
2 
3 // Copyright (C) 2001-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/stdexcept_except.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{stdexcept} */
28 
29 #ifndef _STDEXCEPT_EXCEPT_H
30 #define _STDEXCEPT_EXCEPT_H 1
31 
32 #include <exception>
33 #include <string>
34 
35 namespace std _GLIBCXX_VISIBILITY(default)
36 {
37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 
39 #if _GLIBCXX_USE_DUAL_ABI
40 #if _GLIBCXX_USE_CXX11_ABI
41 #if __cpp_lib_constexpr_exceptions >= 202502L
42  struct __cow_constexpr_string;
43  namespace __detail
44  {
45  extern "C"
46  {
47  void _ZNSt12__cow_stringC2EPKcm(__cow_constexpr_string*, const char*,
48  unsigned long);
49  void _ZNSt12__cow_stringC2EPKcj(__cow_constexpr_string*, const char*,
50  unsigned int);
51  void _ZNSt12__cow_stringC2EPKcy(__cow_constexpr_string*, const char*,
52  unsigned long long);
53  void _ZNSt12__cow_stringC2EPKc(__cow_constexpr_string*, const char*);
54  void _ZNSt12__cow_stringC2ERKS_(__cow_constexpr_string*,
55  const __cow_constexpr_string&) noexcept;
56  void _ZNSt12__cow_stringC2EOS_(__cow_constexpr_string*,
57  __cow_constexpr_string&&) noexcept;
58  void _ZNSt12__cow_stringD2Ev(__cow_constexpr_string*);
59  __cow_constexpr_string&
60  _ZNSt12__cow_stringaSERKS_(__cow_constexpr_string*,
61  const __cow_constexpr_string&) noexcept;
62  __cow_constexpr_string&
63  _ZNSt12__cow_stringaSEOS_(__cow_constexpr_string*,
64  __cow_constexpr_string&&) noexcept;
65  const char*
66  _ZNKSt12__cow_string5c_strEv(const __cow_constexpr_string*) noexcept;
67  }
68  } // namespace __detail
69 
70  // Emulates an old COW string when the new std::string is in use,
71  // but in addition is constexpr and uses the __cow_string out of
72  // line cdtors/methods unless manifestly constant evaluated.
73  struct __cow_constexpr_string
74  {
75  union {
76  const char* _M_p;
77  char _M_bytes[sizeof(const char*)];
78  string* _M_str;
79  };
80 
81  [[__gnu__::__always_inline__]] constexpr
82  __cow_constexpr_string(const string& __o)
83  {
84  if consteval {
85  _M_str = new string(__o);
86  } else {
87  __cow_constexpr_string_ctor(__o.c_str(), __o.length());
88  }
89  }
90 
91  [[__gnu__::__always_inline__]] inline void
92  __cow_constexpr_string_ctor(const char *__s, unsigned long __l)
93  {
94  __detail::_ZNSt12__cow_stringC2EPKcm(this, __s, __l);
95  }
96 
97  [[__gnu__::__always_inline__]] inline void
98  __cow_constexpr_string_ctor(const char *__s, unsigned int __l)
99  {
100  __detail::_ZNSt12__cow_stringC2EPKcj(this, __s, __l);
101  }
102 
103  [[__gnu__::__always_inline__]] inline void
104  __cow_constexpr_string_ctor(const char *__s, unsigned long long __l)
105  {
106  __detail::_ZNSt12__cow_stringC2EPKcy(this, __s, __l);
107  }
108 
109  [[__gnu__::__always_inline__]] constexpr
110  __cow_constexpr_string(const char* __o)
111  {
112  if consteval {
113  _M_str = new string(__o);
114  } else {
115  __detail::_ZNSt12__cow_stringC2EPKc(this, __o);
116  }
117  }
118 
119  [[__gnu__::__always_inline__]] constexpr
120  __cow_constexpr_string(const __cow_constexpr_string& __o) noexcept
121  {
122  if consteval {
123  _M_str = new string(*__o._M_str);
124  } else {
125  __detail::_ZNSt12__cow_stringC2ERKS_(this, __o);
126  }
127  }
128 
129  [[__gnu__::__always_inline__]] constexpr __cow_constexpr_string&
130  operator=(const __cow_constexpr_string& __o) noexcept
131  {
132  if consteval {
133  string* __p = _M_str;
134  _M_str = new string(*__o._M_str);
135  delete __p;
136  return *this;
137  } else {
138  return __detail::_ZNSt12__cow_stringaSERKS_(this, __o);
139  }
140  }
141 
142  [[__gnu__::__always_inline__]] constexpr
143  ~__cow_constexpr_string()
144  {
145  if consteval {
146  delete _M_str;
147  } else {
148  __detail::_ZNSt12__cow_stringD2Ev(this);
149  }
150  }
151 
152  [[__gnu__::__always_inline__]] constexpr
153  __cow_constexpr_string(__cow_constexpr_string&& __o) noexcept
154  {
155  if consteval {
156  _M_str = new string(std::move(*__o._M_str));
157  } else {
158  __detail::_ZNSt12__cow_stringC2EOS_(this, std::move(__o));
159  }
160  }
161 
162  [[__gnu__::__always_inline__]] constexpr __cow_constexpr_string&
163  operator=(__cow_constexpr_string&& __o) noexcept
164  {
165  if consteval {
166  string* __p = _M_str;
167  _M_str = new string(std::move(*__o._M_str));
168  delete __p;
169  return *this;
170  } else {
171  return __detail::_ZNSt12__cow_stringaSEOS_(this, std::move(__o));
172  }
173  }
174 
175  [[__gnu__::__always_inline__]] constexpr const char*
176  c_str() const noexcept
177  {
178  if consteval {
179  return _M_str->c_str();
180  } else {
181  return __detail::_ZNKSt12__cow_string5c_strEv(this);
182  }
183  }
184  };
185 
186  typedef __cow_constexpr_string __cow_string;
187 #else
188  // Emulates an old COW string when the new std::string is in use.
189  struct __cow_string
190  {
191  union {
192  const char* _M_p;
193  char _M_bytes[sizeof(const char*)];
194  };
195 
196  __cow_string();
197  __cow_string(const std::string&);
198  __cow_string(const char*, size_t);
199  __cow_string(const __cow_string&) _GLIBCXX_NOTHROW;
200  __cow_string& operator=(const __cow_string&) _GLIBCXX_NOTHROW;
201  ~__cow_string();
202 #if __cplusplus >= 201103L
203  __cow_string(__cow_string&&) noexcept;
204  __cow_string& operator=(__cow_string&&) noexcept;
205 #endif
206  };
207 #endif
208 
209  typedef basic_string<char> __sso_string;
210 #else // _GLIBCXX_USE_CXX11_ABI
211  typedef basic_string<char> __cow_string;
212 
213  // Emulates a new SSO string when the old std::string is in use.
214  struct __sso_string
215  {
216  struct __str
217  {
218  const char* _M_p;
219  size_t _M_string_length;
220  char _M_local_buf[16];
221  };
222 
223  union {
224  __str _M_s;
225  char _M_bytes[sizeof(__str)];
226  };
227 
228  __sso_string() _GLIBCXX_NOTHROW;
229  __sso_string(const std::string&);
230  __sso_string(const char*, size_t);
231  __sso_string(const __sso_string&);
232  __sso_string& operator=(const __sso_string&);
233  ~__sso_string();
234 #if __cplusplus >= 201103L
235  __sso_string(__sso_string&&) noexcept;
236  __sso_string& operator=(__sso_string&&) noexcept;
237 #endif
238  };
239 #endif // _GLIBCXX_USE_CXX11_ABI
240 #else // _GLIBCXX_USE_DUAL_ABI
241  typedef basic_string<char> __sso_string;
242  typedef basic_string<char> __cow_string;
243 #endif
244 
245  /**
246  * @addtogroup exceptions
247  * @{
248  */
249 
250  /** Logic errors represent problems in the internal logic of a program;
251  * in theory, these are preventable, and even detectable before the
252  * program runs (e.g., violations of class invariants).
253  * @brief One of two subclasses of exception.
254  */
255  class logic_error : public exception
256  {
257  __cow_string _M_msg;
258 
259  public:
260 #if __cpp_lib_constexpr_exceptions >= 202502L
261  [[__gnu__::__gnu_inline__]]
262  constexpr inline explicit
263  logic_error(const string& __arg) _GLIBCXX_TXN_SAFE
264  : _M_msg(__arg) {}
265 
266  [[__gnu__::__gnu_inline__]]
267  constexpr inline explicit
268  logic_error(const char* __arg) _GLIBCXX_TXN_SAFE
269  : _M_msg(__arg) {}
270 
271  constexpr logic_error(logic_error&& __arg) noexcept = default;
272  constexpr logic_error& operator=(logic_error&& __arg) noexcept = default;
273  constexpr logic_error(const logic_error&) noexcept = default;
274  constexpr logic_error& operator=(const logic_error&) noexcept = default;
275 
276  [[__gnu__::__gnu_inline__]]
277  constexpr inline virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN noexcept { }
278 
279  [[__gnu__::__gnu_inline__]]
280  constexpr inline virtual const char*
281  what() const _GLIBCXX_TXN_SAFE_DYN noexcept
282  {
283  return _M_msg.c_str();
284  }
285 #else
286  /** Takes a character string describing the error. */
287  explicit
288  logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
289 
290 #if __cplusplus >= 201103L
291  explicit
292  logic_error(const char*) _GLIBCXX_TXN_SAFE;
293 
294  logic_error(logic_error&&) noexcept;
295  logic_error& operator=(logic_error&&) noexcept;
296 #endif
297 
298 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
299  logic_error(const logic_error&) _GLIBCXX_NOTHROW;
300  logic_error& operator=(const logic_error&) _GLIBCXX_NOTHROW;
301 #elif __cplusplus >= 201103L
302  logic_error(const logic_error&) = default;
303  logic_error& operator=(const logic_error&) = default;
304 #endif
305 
306  virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
307 
308  /** Returns a C-style character string describing the general cause of
309  * the current error (the same string passed to the ctor). */
310  virtual const char*
311  what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
312 #endif
313 
314 # ifdef _GLIBCXX_TM_TS_INTERNAL
315  friend void*
316  ::_txnal_logic_error_get_msg(void* e);
317 # endif
318  };
319 
320  /** Thrown by the library, or by you, to report domain errors (domain in
321  * the mathematical sense). */
322  class domain_error : public logic_error
323  {
324  public:
325 #if __cpp_lib_constexpr_exceptions >= 202502L
326  [[__gnu__::__gnu_inline__]]
327  constexpr inline explicit domain_error(const string& __arg)
328  _GLIBCXX_TXN_SAFE
329  : logic_error(__arg) { }
330  [[__gnu__::__gnu_inline__]]
331  constexpr inline explicit domain_error(const char* __arg) _GLIBCXX_TXN_SAFE
332  : logic_error(__arg) { }
333  constexpr domain_error(const domain_error&) = default;
334  constexpr domain_error& operator=(const domain_error&) = default;
335  constexpr domain_error(domain_error&&) = default;
336  constexpr domain_error& operator=(domain_error&&) = default;
337  [[__gnu__::__gnu_inline__]]
338  constexpr inline virtual ~domain_error() noexcept { }
339 #else
340  explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE;
341 #if __cplusplus >= 201103L
342  explicit domain_error(const char*) _GLIBCXX_TXN_SAFE;
343  domain_error(const domain_error&) = default;
344  domain_error& operator=(const domain_error&) = default;
345  domain_error(domain_error&&) = default;
346  domain_error& operator=(domain_error&&) = default;
347 #endif
348  virtual ~domain_error() _GLIBCXX_NOTHROW;
349 #endif
350  };
351 
352  /** Thrown to report invalid arguments to functions. */
354  {
355  public:
356 #if __cpp_lib_constexpr_exceptions >= 202502L
357  [[__gnu__::__gnu_inline__]]
358  constexpr inline explicit invalid_argument(const string& __arg)
359  _GLIBCXX_TXN_SAFE
360  : logic_error(__arg) { }
361  [[__gnu__::__gnu_inline__]]
362  constexpr inline explicit invalid_argument(const char* __arg)
363  _GLIBCXX_TXN_SAFE
364  : logic_error(__arg) { }
365  constexpr invalid_argument(const invalid_argument&) = default;
366  constexpr invalid_argument& operator=(const invalid_argument&) = default;
367  constexpr invalid_argument(invalid_argument&&) = default;
368  constexpr invalid_argument& operator=(invalid_argument&&) = default;
369  [[__gnu__::__gnu_inline__]]
370  constexpr inline virtual ~invalid_argument() noexcept { }
371 #else
372  explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE;
373 #if __cplusplus >= 201103L
374  explicit invalid_argument(const char*) _GLIBCXX_TXN_SAFE;
375  invalid_argument(const invalid_argument&) = default;
376  invalid_argument& operator=(const invalid_argument&) = default;
377  invalid_argument(invalid_argument&&) = default;
378  invalid_argument& operator=(invalid_argument&&) = default;
379 #endif
380  virtual ~invalid_argument() _GLIBCXX_NOTHROW;
381 #endif
382  };
383 
384  /** Thrown when an object is constructed that would exceed its maximum
385  * permitted size (e.g., a basic_string instance). */
386  class length_error : public logic_error
387  {
388  public:
389 #if __cpp_lib_constexpr_exceptions >= 202502L
390  [[__gnu__::__gnu_inline__]]
391  constexpr inline explicit length_error(const string& __arg)
392  _GLIBCXX_TXN_SAFE
393  : logic_error(__arg) { }
394  [[__gnu__::__gnu_inline__]]
395  constexpr inline explicit length_error(const char* __arg) _GLIBCXX_TXN_SAFE
396  : logic_error(__arg) { }
397  constexpr length_error(const length_error&) = default;
398  constexpr length_error& operator=(const length_error&) = default;
399  constexpr length_error(length_error&&) = default;
400  constexpr length_error& operator=(length_error&&) = default;
401  [[__gnu__::__gnu_inline__]]
402  constexpr inline virtual ~length_error() noexcept { }
403 #else
404  explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE;
405 #if __cplusplus >= 201103L
406  explicit length_error(const char*) _GLIBCXX_TXN_SAFE;
407  length_error(const length_error&) = default;
408  length_error& operator=(const length_error&) = default;
409  length_error(length_error&&) = default;
410  length_error& operator=(length_error&&) = default;
411 #endif
412  virtual ~length_error() _GLIBCXX_NOTHROW;
413 #endif
414  };
415 
416  /** This represents an argument whose value is not within the expected
417  * range (e.g., boundary checks in basic_string). */
418  class out_of_range : public logic_error
419  {
420  public:
421 #if __cpp_lib_constexpr_exceptions >= 202502L
422  [[__gnu__::__gnu_inline__]]
423  constexpr inline explicit out_of_range(const string& __arg)
424  _GLIBCXX_TXN_SAFE
425  : logic_error(__arg) { }
426  [[__gnu__::__gnu_inline__]]
427  constexpr inline explicit out_of_range(const char* __arg) _GLIBCXX_TXN_SAFE
428  : logic_error(__arg) { }
429  constexpr out_of_range(const out_of_range&) = default;
430  constexpr out_of_range& operator=(const out_of_range&) = default;
431  constexpr out_of_range(out_of_range&&) = default;
432  constexpr out_of_range& operator=(out_of_range&&) = default;
433  [[__gnu__::__gnu_inline__]]
434  constexpr inline virtual ~out_of_range() noexcept { }
435 #else
436  explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE;
437 #if __cplusplus >= 201103L
438  explicit out_of_range(const char*) _GLIBCXX_TXN_SAFE;
439  out_of_range(const out_of_range&) = default;
440  out_of_range& operator=(const out_of_range&) = default;
441  out_of_range(out_of_range&&) = default;
442  out_of_range& operator=(out_of_range&&) = default;
443 #endif
444  virtual ~out_of_range() _GLIBCXX_NOTHROW;
445 #endif
446  };
447 
448  /** Runtime errors represent problems outside the scope of a program;
449  * they cannot be easily predicted and can generally only be caught as
450  * the program executes.
451  * @brief One of two subclasses of exception.
452  */
453  class runtime_error : public exception
454  {
455  __cow_string _M_msg;
456 
457  public:
458 #if __cpp_lib_constexpr_exceptions >= 202502L
459  [[__gnu__::__gnu_inline__]]
460  constexpr inline explicit
461  runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE
462  : _M_msg(__arg) {}
463 
464  [[__gnu__::__gnu_inline__]]
465  constexpr inline explicit
466  runtime_error(const char* __arg) _GLIBCXX_TXN_SAFE
467  : _M_msg(__arg) {}
468 
469  constexpr runtime_error(runtime_error&&) noexcept = default;
470  constexpr runtime_error& operator=(runtime_error&&) noexcept = default;
471  constexpr runtime_error(const runtime_error&) noexcept = default;
472  runtime_error& operator=(const runtime_error&) noexcept = default;
473 
474  [[__gnu__::__gnu_inline__]]
475  constexpr inline virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN noexcept
476  {}
477 
478  [[__gnu__::__gnu_inline__]]
479  constexpr inline virtual const char*
480  what() const _GLIBCXX_TXN_SAFE_DYN noexcept
481  {
482  return _M_msg.c_str();
483  }
484 #else
485  /** Takes a character string describing the error. */
486  explicit
487  runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE;
488 
489 #if __cplusplus >= 201103L
490  explicit
491  runtime_error(const char*) _GLIBCXX_TXN_SAFE;
492 
493  runtime_error(runtime_error&&) noexcept;
494  runtime_error& operator=(runtime_error&&) noexcept;
495 #endif
496 
497 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
498  runtime_error(const runtime_error&) _GLIBCXX_NOTHROW;
499  runtime_error& operator=(const runtime_error&) _GLIBCXX_NOTHROW;
500 #elif __cplusplus >= 201103L
501  runtime_error(const runtime_error&) = default;
502  runtime_error& operator=(const runtime_error&) = default;
503 #endif
504 
505  virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
506 
507  /** Returns a C-style character string describing the general cause of
508  * the current error (the same string passed to the ctor). */
509  virtual const char*
510  what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
511 #endif
512 
513 # ifdef _GLIBCXX_TM_TS_INTERNAL
514  friend void*
515  ::_txnal_runtime_error_get_msg(void* e);
516 # endif
517  };
518 
519  /** Thrown to indicate arithmetic overflow. */
521  {
522  public:
523 #if __cpp_lib_constexpr_exceptions >= 202502L
524  [[__gnu__::__gnu_inline__]]
525  constexpr inline explicit overflow_error(const string& __arg)
526  _GLIBCXX_TXN_SAFE
527  : runtime_error(__arg) { }
528  [[__gnu__::__gnu_inline__]]
529  constexpr inline explicit overflow_error(const char* __arg)
530  _GLIBCXX_TXN_SAFE
531  : runtime_error(__arg) { }
532  constexpr overflow_error(const overflow_error&) = default;
533  constexpr overflow_error& operator=(const overflow_error&) = default;
534  constexpr overflow_error(overflow_error&&) = default;
535  constexpr overflow_error& operator=(overflow_error&&) = default;
536  [[__gnu__::__gnu_inline__]]
537  constexpr inline virtual ~overflow_error() noexcept { }
538 #else
539  explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
540 #if __cplusplus >= 201103L
541  explicit overflow_error(const char*) _GLIBCXX_TXN_SAFE;
542  overflow_error(const overflow_error&) = default;
543  overflow_error& operator=(const overflow_error&) = default;
544  overflow_error(overflow_error&&) = default;
545  overflow_error& operator=(overflow_error&&) = default;
546 #endif
547  virtual ~overflow_error() _GLIBCXX_NOTHROW;
548 #endif
549  };
550 
551  /** Thrown to indicate arithmetic underflow. */
553  {
554  public:
555 #if __cpp_lib_constexpr_exceptions >= 202502L
556  [[__gnu__::__gnu_inline__]]
557  constexpr inline explicit underflow_error(const string& __arg)
558  _GLIBCXX_TXN_SAFE
559  : runtime_error(__arg) { }
560  [[__gnu__::__gnu_inline__]]
561  constexpr inline explicit underflow_error(const char* __arg)
562  _GLIBCXX_TXN_SAFE
563  : runtime_error(__arg) { }
564  constexpr underflow_error(const underflow_error&) = default;
565  constexpr underflow_error& operator=(const underflow_error&) = default;
566  constexpr underflow_error(underflow_error&&) = default;
567  constexpr underflow_error& operator=(underflow_error&&) = default;
568  [[__gnu__::__gnu_inline__]]
569  constexpr inline virtual ~underflow_error() noexcept { }
570 #else
571  explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
572 #if __cplusplus >= 201103L
573  explicit underflow_error(const char*) _GLIBCXX_TXN_SAFE;
574  underflow_error(const underflow_error&) = default;
575  underflow_error& operator=(const underflow_error&) = default;
576  underflow_error(underflow_error&&) = default;
577  underflow_error& operator=(underflow_error&&) = default;
578 #endif
579  virtual ~underflow_error() _GLIBCXX_NOTHROW;
580 #endif
581  };
582 
583  /// @} group exceptions
584 
585 _GLIBCXX_END_NAMESPACE_VERSION
586 } // namespace
587 
588 #endif /* _STDEXCEPT_EXCEPT_H */
std::domain_error
Definition: stdexcept_except.h:322
std::runtime_error
One of two subclasses of exception.
Definition: stdexcept_except.h:453
string
std::length_error
Definition: stdexcept_except.h:386
std::logic_error
One of two subclasses of exception.
Definition: stdexcept_except.h:255
std::string
basic_string< char > string
A string of char.
Definition: stringfwd.h:74
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
std::exception
Base class for all library exceptions.
Definition: exception.h:61
std
ISO C++ entities toplevel namespace is std.
std::underflow_error
Definition: stdexcept_except.h:552
exception
std::invalid_argument
Definition: stdexcept_except.h:353
std::basic_string< char >
std::overflow_error
Definition: stdexcept_except.h:520
std::out_of_range
Definition: stdexcept_except.h:418