libstdc++
stdexcept_except.h
Go to the documentation of this file.
1// Exception classes for <stdexcept> -*- C++ -*-
2
3// Copyright (C) 2001-2025 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
35namespace 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 constexpr explicit
262 logic_error(const string& __arg) _GLIBCXX_TXN_SAFE
263 : _M_msg(__arg) {}
264
265 constexpr explicit
266 logic_error(const char* __arg) _GLIBCXX_TXN_SAFE
267 : _M_msg(__arg) {}
268
269 constexpr logic_error(logic_error&& __arg) noexcept = default;
270 constexpr logic_error& operator=(logic_error&& __arg) noexcept = default;
271 constexpr logic_error(const logic_error&) noexcept = default;
272 constexpr logic_error& operator=(const logic_error&) noexcept = default;
273
274 constexpr virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN noexcept { }
275
276 constexpr virtual const char*
277 what() const _GLIBCXX_TXN_SAFE_DYN noexcept
278 {
279 return _M_msg.c_str();
280 }
281#else
282 /** Takes a character string describing the error. */
283 explicit
284 logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
285
286#if __cplusplus >= 201103L
287 explicit
288 logic_error(const char*) _GLIBCXX_TXN_SAFE;
289
290 logic_error(logic_error&&) noexcept;
291 logic_error& operator=(logic_error&&) noexcept;
292#endif
293
294#if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
295 logic_error(const logic_error&) _GLIBCXX_NOTHROW;
296 logic_error& operator=(const logic_error&) _GLIBCXX_NOTHROW;
297#elif __cplusplus >= 201103L
298 logic_error(const logic_error&) = default;
299 logic_error& operator=(const logic_error&) = default;
300#endif
301
302 virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
303
304 /** Returns a C-style character string describing the general cause of
305 * the current error (the same string passed to the ctor). */
306 virtual const char*
307 what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
308#endif
309
310# ifdef _GLIBCXX_TM_TS_INTERNAL
311 friend void*
312 ::_txnal_logic_error_get_msg(void* e);
313# endif
314 };
315
316 /** Thrown by the library, or by you, to report domain errors (domain in
317 * the mathematical sense). */
318 class domain_error : public logic_error
319 {
320 public:
321#if __cpp_lib_constexpr_exceptions >= 202502L
322 constexpr explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE
323 : logic_error(__arg) { }
324 constexpr explicit domain_error(const char* __arg) _GLIBCXX_TXN_SAFE
325 : logic_error(__arg) { }
326 constexpr domain_error(const domain_error&) = default;
327 constexpr domain_error& operator=(const domain_error&) = default;
328 constexpr domain_error(domain_error&&) = default;
329 constexpr domain_error& operator=(domain_error&&) = default;
330 constexpr virtual ~domain_error() _GLIBCXX_NOTHROW { }
331#else
332 explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE;
333#if __cplusplus >= 201103L
334 explicit domain_error(const char*) _GLIBCXX_TXN_SAFE;
335 domain_error(const domain_error&) = default;
336 domain_error& operator=(const domain_error&) = default;
337 domain_error(domain_error&&) = default;
338 domain_error& operator=(domain_error&&) = default;
339#endif
340 virtual ~domain_error() _GLIBCXX_NOTHROW;
341#endif
342 };
343
344 /** Thrown to report invalid arguments to functions. */
345 class invalid_argument : public logic_error
346 {
347 public:
348#if __cpp_lib_constexpr_exceptions >= 202502L
349 constexpr explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE
350 : logic_error(__arg) { }
351 constexpr explicit invalid_argument(const char* __arg) _GLIBCXX_TXN_SAFE
352 : logic_error(__arg) { }
353 constexpr invalid_argument(const invalid_argument&) = default;
354 constexpr invalid_argument& operator=(const invalid_argument&) = default;
355 constexpr invalid_argument(invalid_argument&&) = default;
356 constexpr invalid_argument& operator=(invalid_argument&&) = default;
357 constexpr virtual ~invalid_argument() _GLIBCXX_NOTHROW { }
358#else
359 explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE;
360#if __cplusplus >= 201103L
361 explicit invalid_argument(const char*) _GLIBCXX_TXN_SAFE;
362 invalid_argument(const invalid_argument&) = default;
363 invalid_argument& operator=(const invalid_argument&) = default;
364 invalid_argument(invalid_argument&&) = default;
365 invalid_argument& operator=(invalid_argument&&) = default;
366#endif
367 virtual ~invalid_argument() _GLIBCXX_NOTHROW;
368#endif
369 };
370
371 /** Thrown when an object is constructed that would exceed its maximum
372 * permitted size (e.g., a basic_string instance). */
373 class length_error : public logic_error
374 {
375 public:
376#if __cpp_lib_constexpr_exceptions >= 202502L
377 constexpr explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE
378 : logic_error(__arg) { }
379 constexpr explicit length_error(const char* __arg) _GLIBCXX_TXN_SAFE
380 : logic_error(__arg) { }
381 constexpr length_error(const length_error&) = default;
382 constexpr length_error& operator=(const length_error&) = default;
383 constexpr length_error(length_error&&) = default;
384 constexpr length_error& operator=(length_error&&) = default;
385 constexpr virtual ~length_error() _GLIBCXX_NOTHROW { }
386#else
387 explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE;
388#if __cplusplus >= 201103L
389 explicit length_error(const char*) _GLIBCXX_TXN_SAFE;
390 length_error(const length_error&) = default;
391 length_error& operator=(const length_error&) = default;
392 length_error(length_error&&) = default;
393 length_error& operator=(length_error&&) = default;
394#endif
395 virtual ~length_error() _GLIBCXX_NOTHROW;
396#endif
397 };
398
399 /** This represents an argument whose value is not within the expected
400 * range (e.g., boundary checks in basic_string). */
401 class out_of_range : public logic_error
402 {
403 public:
404#if __cpp_lib_constexpr_exceptions >= 202502L
405 constexpr explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE
406 : logic_error(__arg) { }
407 constexpr explicit out_of_range(const char* __arg) _GLIBCXX_TXN_SAFE
408 : logic_error(__arg) { }
409 constexpr out_of_range(const out_of_range&) = default;
410 constexpr out_of_range& operator=(const out_of_range&) = default;
411 constexpr out_of_range(out_of_range&&) = default;
412 constexpr out_of_range& operator=(out_of_range&&) = default;
413 constexpr virtual ~out_of_range() _GLIBCXX_NOTHROW { }
414#else
415 explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE;
416#if __cplusplus >= 201103L
417 explicit out_of_range(const char*) _GLIBCXX_TXN_SAFE;
418 out_of_range(const out_of_range&) = default;
419 out_of_range& operator=(const out_of_range&) = default;
420 out_of_range(out_of_range&&) = default;
421 out_of_range& operator=(out_of_range&&) = default;
422#endif
423 virtual ~out_of_range() _GLIBCXX_NOTHROW;
424#endif
425 };
426
427 /** Runtime errors represent problems outside the scope of a program;
428 * they cannot be easily predicted and can generally only be caught as
429 * the program executes.
430 * @brief One of two subclasses of exception.
431 */
432 class runtime_error : public exception
433 {
434 __cow_string _M_msg;
435
436 public:
437#if __cpp_lib_constexpr_exceptions >= 202502L
438 constexpr explicit
439 runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE
440 : _M_msg(__arg) {}
441
442 constexpr explicit
443 runtime_error(const char* __arg) _GLIBCXX_TXN_SAFE
444 : _M_msg(__arg) {}
445
446 constexpr runtime_error(runtime_error&&) noexcept = default;
447 constexpr runtime_error& operator=(runtime_error&&) noexcept = default;
448 constexpr runtime_error(const runtime_error&) noexcept = default;
449 runtime_error& operator=(const runtime_error&) noexcept = default;
450
451 constexpr virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN noexcept { }
452
453 constexpr virtual const char*
454 what() const _GLIBCXX_TXN_SAFE_DYN noexcept
455 {
456 return _M_msg.c_str();
457 }
458#else
459 /** Takes a character string describing the error. */
460 explicit
461 runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE;
462
463#if __cplusplus >= 201103L
464 explicit
465 runtime_error(const char*) _GLIBCXX_TXN_SAFE;
466
467 runtime_error(runtime_error&&) noexcept;
468 runtime_error& operator=(runtime_error&&) noexcept;
469#endif
470
471#if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
472 runtime_error(const runtime_error&) _GLIBCXX_NOTHROW;
473 runtime_error& operator=(const runtime_error&) _GLIBCXX_NOTHROW;
474#elif __cplusplus >= 201103L
475 runtime_error(const runtime_error&) = default;
476 runtime_error& operator=(const runtime_error&) = default;
477#endif
478
479 virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
480
481 /** Returns a C-style character string describing the general cause of
482 * the current error (the same string passed to the ctor). */
483 virtual const char*
484 what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
485#endif
486
487# ifdef _GLIBCXX_TM_TS_INTERNAL
488 friend void*
489 ::_txnal_runtime_error_get_msg(void* e);
490# endif
491 };
492
493 /** Thrown to indicate arithmetic overflow. */
494 class overflow_error : public runtime_error
495 {
496 public:
497#if __cpp_lib_constexpr_exceptions >= 202502L
498 constexpr explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE
499 : runtime_error(__arg) { }
500 constexpr explicit overflow_error(const char* __arg) _GLIBCXX_TXN_SAFE
501 : runtime_error(__arg) { }
502 constexpr overflow_error(const overflow_error&) = default;
503 constexpr overflow_error& operator=(const overflow_error&) = default;
504 constexpr overflow_error(overflow_error&&) = default;
505 constexpr overflow_error& operator=(overflow_error&&) = default;
506 constexpr virtual ~overflow_error() noexcept { }
507#else
508 explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
509#if __cplusplus >= 201103L
510 explicit overflow_error(const char*) _GLIBCXX_TXN_SAFE;
511 overflow_error(const overflow_error&) = default;
512 overflow_error& operator=(const overflow_error&) = default;
513 overflow_error(overflow_error&&) = default;
514 overflow_error& operator=(overflow_error&&) = default;
515#endif
516 virtual ~overflow_error() _GLIBCXX_NOTHROW;
517#endif
518 };
519
520 /** Thrown to indicate arithmetic underflow. */
521 class underflow_error : public runtime_error
522 {
523 public:
524#if __cpp_lib_constexpr_exceptions >= 202502L
525 constexpr explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE
526 : runtime_error(__arg) { }
527 constexpr explicit underflow_error(const char* __arg) _GLIBCXX_TXN_SAFE
528 : runtime_error(__arg) { }
529 constexpr underflow_error(const underflow_error&) = default;
530 constexpr underflow_error& operator=(const underflow_error&) = default;
531 constexpr underflow_error(underflow_error&&) = default;
532 constexpr underflow_error& operator=(underflow_error&&) = default;
533 constexpr virtual ~underflow_error() noexcept { }
534#else
535 explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
536#if __cplusplus >= 201103L
537 explicit underflow_error(const char*) _GLIBCXX_TXN_SAFE;
538 underflow_error(const underflow_error&) = default;
539 underflow_error& operator=(const underflow_error&) = default;
540 underflow_error(underflow_error&&) = default;
541 underflow_error& operator=(underflow_error&&) = default;
542#endif
543 virtual ~underflow_error() _GLIBCXX_NOTHROW;
544#endif
545 };
546
547 /// @} group exceptions
548
549_GLIBCXX_END_NAMESPACE_VERSION
550} // namespace
551
552#endif /* _STDEXCEPT_EXCEPT_H */
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
basic_string< char > string
A string of char.
Definition stringfwd.h:79
ISO C++ entities toplevel namespace is std.
Implementation details not part of the namespace std interface.
virtual const char * what() const noexcept
logic_error(const string &__arg)
runtime_error(const string &__arg)
virtual const char * what() const noexcept