libstdc++
stdexcept
Go to the documentation of this file.
1 // Standard exception classes -*- 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 include/stdexcept
26  * This is a Standard C++ Library header.
27  */
28 
29 //
30 // ISO C++ 19.1 Exception classes
31 //
32 
33 #ifndef _GLIBCXX_STDEXCEPT
34 #define _GLIBCXX_STDEXCEPT 1
35 
36 #ifdef _GLIBCXX_SYSHDR
37 #pragma GCC system_header
38 #endif
39 
40 #include <exception>
41 #include <string>
42 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
47 #if _GLIBCXX_USE_DUAL_ABI
48 #if _GLIBCXX_USE_CXX11_ABI
49  // Emulates an old COW string when the new std::string is in use.
50  struct __cow_string
51  {
52  union {
53  const char* _M_p;
54  char _M_bytes[sizeof(const char*)];
55  };
56 
57  __cow_string();
58  __cow_string(const std::string&);
59  __cow_string(const char*, size_t);
60  __cow_string(const __cow_string&) _GLIBCXX_NOTHROW;
61  __cow_string& operator=(const __cow_string&) _GLIBCXX_NOTHROW;
62  ~__cow_string();
63 #if __cplusplus >= 201103L
64  __cow_string(__cow_string&&) noexcept;
65  __cow_string& operator=(__cow_string&&) noexcept;
66 #endif
67  };
68 
69  typedef basic_string<char> __sso_string;
70 #else // _GLIBCXX_USE_CXX11_ABI
71  typedef basic_string<char> __cow_string;
72 
73  // Emulates a new SSO string when the old std::string is in use.
74  struct __sso_string
75  {
76  struct __str
77  {
78  const char* _M_p;
79  size_t _M_string_length;
80  char _M_local_buf[16];
81  };
82 
83  union {
84  __str _M_s;
85  char _M_bytes[sizeof(__str)];
86  };
87 
88  __sso_string() _GLIBCXX_NOTHROW;
89  __sso_string(const std::string&);
90  __sso_string(const char*, size_t);
91  __sso_string(const __sso_string&);
92  __sso_string& operator=(const __sso_string&);
93  ~__sso_string();
94 #if __cplusplus >= 201103L
95  __sso_string(__sso_string&&) noexcept;
96  __sso_string& operator=(__sso_string&&) noexcept;
97 #endif
98  };
99 #endif // _GLIBCXX_USE_CXX11_ABI
100 #else // _GLIBCXX_USE_DUAL_ABI
101  typedef basic_string<char> __sso_string;
102  typedef basic_string<char> __cow_string;
103 #endif
104 
105  /**
106  * @addtogroup exceptions
107  * @{
108  */
109 
110  /** Logic errors represent problems in the internal logic of a program;
111  * in theory, these are preventable, and even detectable before the
112  * program runs (e.g., violations of class invariants).
113  * @brief One of two subclasses of exception.
114  */
115  class logic_error : public exception
116  {
117  __cow_string _M_msg;
118 
119  public:
120  /** Takes a character string describing the error. */
121  explicit
122  logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
123 
124 #if __cplusplus >= 201103L
125  explicit
126  logic_error(const char*) _GLIBCXX_TXN_SAFE;
127 
128  logic_error(logic_error&&) noexcept;
129  logic_error& operator=(logic_error&&) noexcept;
130 #endif
131 
132 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
133  logic_error(const logic_error&) _GLIBCXX_NOTHROW;
134  logic_error& operator=(const logic_error&) _GLIBCXX_NOTHROW;
135 #elif __cplusplus >= 201103L
136  logic_error(const logic_error&) = default;
137  logic_error& operator=(const logic_error&) = default;
138 #endif
139 
140  virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
141 
142  /** Returns a C-style character string describing the general cause of
143  * the current error (the same string passed to the ctor). */
144  virtual const char*
145  what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
146 
147 # ifdef _GLIBCXX_TM_TS_INTERNAL
148  friend void*
149  ::_txnal_logic_error_get_msg(void* e);
150 # endif
151  };
152 
153  /** Thrown by the library, or by you, to report domain errors (domain in
154  * the mathematical sense). */
155  class domain_error : public logic_error
156  {
157  public:
158  explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE;
159 #if __cplusplus >= 201103L
160  explicit domain_error(const char*) _GLIBCXX_TXN_SAFE;
161  domain_error(const domain_error&) = default;
162  domain_error& operator=(const domain_error&) = default;
163  domain_error(domain_error&&) = default;
164  domain_error& operator=(domain_error&&) = default;
165 #endif
166  virtual ~domain_error() _GLIBCXX_NOTHROW;
167  };
168 
169  /** Thrown to report invalid arguments to functions. */
170  class invalid_argument : public logic_error
171  {
172  public:
173  explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE;
174 #if __cplusplus >= 201103L
175  explicit invalid_argument(const char*) _GLIBCXX_TXN_SAFE;
176  invalid_argument(const invalid_argument&) = default;
177  invalid_argument& operator=(const invalid_argument&) = default;
178  invalid_argument(invalid_argument&&) = default;
179  invalid_argument& operator=(invalid_argument&&) = default;
180 #endif
181  virtual ~invalid_argument() _GLIBCXX_NOTHROW;
182  };
183 
184  /** Thrown when an object is constructed that would exceed its maximum
185  * permitted size (e.g., a basic_string instance). */
186  class length_error : public logic_error
187  {
188  public:
189  explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE;
190 #if __cplusplus >= 201103L
191  explicit length_error(const char*) _GLIBCXX_TXN_SAFE;
192  length_error(const length_error&) = default;
193  length_error& operator=(const length_error&) = default;
194  length_error(length_error&&) = default;
195  length_error& operator=(length_error&&) = default;
196 #endif
197  virtual ~length_error() _GLIBCXX_NOTHROW;
198  };
199 
200  /** This represents an argument whose value is not within the expected
201  * range (e.g., boundary checks in basic_string). */
202  class out_of_range : public logic_error
203  {
204  public:
205  explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE;
206 #if __cplusplus >= 201103L
207  explicit out_of_range(const char*) _GLIBCXX_TXN_SAFE;
208  out_of_range(const out_of_range&) = default;
209  out_of_range& operator=(const out_of_range&) = default;
210  out_of_range(out_of_range&&) = default;
211  out_of_range& operator=(out_of_range&&) = default;
212 #endif
213  virtual ~out_of_range() _GLIBCXX_NOTHROW;
214  };
215 
216  /** Runtime errors represent problems outside the scope of a program;
217  * they cannot be easily predicted and can generally only be caught as
218  * the program executes.
219  * @brief One of two subclasses of exception.
220  */
221  class runtime_error : public exception
222  {
223  __cow_string _M_msg;
224 
225  public:
226  /** Takes a character string describing the error. */
227  explicit
228  runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE;
229 
230 #if __cplusplus >= 201103L
231  explicit
232  runtime_error(const char*) _GLIBCXX_TXN_SAFE;
233 
234  runtime_error(runtime_error&&) noexcept;
235  runtime_error& operator=(runtime_error&&) noexcept;
236 #endif
237 
238 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
239  runtime_error(const runtime_error&) _GLIBCXX_NOTHROW;
240  runtime_error& operator=(const runtime_error&) _GLIBCXX_NOTHROW;
241 #elif __cplusplus >= 201103L
242  runtime_error(const runtime_error&) = default;
243  runtime_error& operator=(const runtime_error&) = default;
244 #endif
245 
246  virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
247 
248  /** Returns a C-style character string describing the general cause of
249  * the current error (the same string passed to the ctor). */
250  virtual const char*
251  what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
252 
253 # ifdef _GLIBCXX_TM_TS_INTERNAL
254  friend void*
255  ::_txnal_runtime_error_get_msg(void* e);
256 # endif
257  };
258 
259  /** Thrown to indicate range errors in internal computations. */
260  class range_error : public runtime_error
261  {
262  public:
263  explicit range_error(const string& __arg) _GLIBCXX_TXN_SAFE;
264 #if __cplusplus >= 201103L
265  explicit range_error(const char*) _GLIBCXX_TXN_SAFE;
266  range_error(const range_error&) = default;
267  range_error& operator=(const range_error&) = default;
268  range_error(range_error&&) = default;
269  range_error& operator=(range_error&&) = default;
270 #endif
271  virtual ~range_error() _GLIBCXX_NOTHROW;
272  };
273 
274  /** Thrown to indicate arithmetic overflow. */
275  class overflow_error : public runtime_error
276  {
277  public:
278  explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
279 #if __cplusplus >= 201103L
280  explicit overflow_error(const char*) _GLIBCXX_TXN_SAFE;
281  overflow_error(const overflow_error&) = default;
282  overflow_error& operator=(const overflow_error&) = default;
283  overflow_error(overflow_error&&) = default;
284  overflow_error& operator=(overflow_error&&) = default;
285 #endif
286  virtual ~overflow_error() _GLIBCXX_NOTHROW;
287  };
288 
289  /** Thrown to indicate arithmetic underflow. */
290  class underflow_error : public runtime_error
291  {
292  public:
293  explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
294 #if __cplusplus >= 201103L
295  explicit underflow_error(const char*) _GLIBCXX_TXN_SAFE;
296  underflow_error(const underflow_error&) = default;
297  underflow_error& operator=(const underflow_error&) = default;
298  underflow_error(underflow_error&&) = default;
299  underflow_error& operator=(underflow_error&&) = default;
300 #endif
301  virtual ~underflow_error() _GLIBCXX_NOTHROW;
302  };
303 
304  /// @} group exceptions
305 
306 _GLIBCXX_END_NAMESPACE_VERSION
307 } // namespace
308 
309 #endif /* _GLIBCXX_STDEXCEPT */