1 // Standard exception classes -*- C++ -*-
3 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file include/stdexcept
26 * This is a Standard C++ Library header.
30 // ISO C++ 19.1 Exception classes
33 #ifndef _GLIBCXX_STDEXCEPT
34 #define _GLIBCXX_STDEXCEPT 1
36 #pragma GCC system_header
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 #if _GLIBCXX_USE_DUAL_ABI
46 #if _GLIBCXX_USE_CXX11_ABI
47 // Emulates an old COW string when the new std::string is in use.
52 char _M_bytes[sizeof(const char*)];
56 __cow_string(const std::string&);
57 __cow_string(const char*, size_t);
58 __cow_string(const __cow_string&) _GLIBCXX_USE_NOEXCEPT;
59 __cow_string& operator=(const __cow_string&) _GLIBCXX_USE_NOEXCEPT;
61 #if __cplusplus >= 201103L
62 __cow_string(__cow_string&&) noexcept;
63 __cow_string& operator=(__cow_string&&) noexcept;
67 typedef basic_string<char> __sso_string;
68 #else // _GLIBCXX_USE_CXX11_ABI
69 typedef basic_string<char> __cow_string;
71 // Emulates a new SSO string when the old std::string is in use.
77 size_t _M_string_length;
78 char _M_local_buf[16];
83 char _M_bytes[sizeof(__str)];
86 __sso_string() _GLIBCXX_USE_NOEXCEPT;
87 __sso_string(const std::string&);
88 __sso_string(const char*, size_t);
89 __sso_string(const __sso_string&);
90 __sso_string& operator=(const __sso_string&);
92 #if __cplusplus >= 201103L
93 __sso_string(__sso_string&&) noexcept;
94 __sso_string& operator=(__sso_string&&) noexcept;
97 #endif // _GLIBCXX_USE_CXX11_ABI
98 #else // _GLIBCXX_USE_DUAL_ABI
99 typedef basic_string<char> __sso_string;
100 typedef basic_string<char> __cow_string;
104 * @addtogroup exceptions
108 /** Logic errors represent problems in the internal logic of a program;
109 * in theory, these are preventable, and even detectable before the
110 * program runs (e.g., violations of class invariants).
111 * @brief One of two subclasses of exception.
113 class logic_error : public exception
118 /** Takes a character string describing the error. */
120 logic_error(const string& __arg);
122 #if __cplusplus >= 201103L
124 logic_error(const char*);
127 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
128 logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
129 logic_error& operator=(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
132 virtual ~logic_error() _GLIBCXX_USE_NOEXCEPT;
134 /** Returns a C-style character string describing the general cause of
135 * the current error (the same string passed to the ctor). */
137 what() const _GLIBCXX_USE_NOEXCEPT;
140 /** Thrown by the library, or by you, to report domain errors (domain in
141 * the mathematical sense). */
142 class domain_error : public logic_error
145 explicit domain_error(const string& __arg);
146 #if __cplusplus >= 201103L
147 explicit domain_error(const char*);
149 virtual ~domain_error() _GLIBCXX_USE_NOEXCEPT;
152 /** Thrown to report invalid arguments to functions. */
153 class invalid_argument : public logic_error
156 explicit invalid_argument(const string& __arg);
157 #if __cplusplus >= 201103L
158 explicit invalid_argument(const char*);
160 virtual ~invalid_argument() _GLIBCXX_USE_NOEXCEPT;
163 /** Thrown when an object is constructed that would exceed its maximum
164 * permitted size (e.g., a basic_string instance). */
165 class length_error : public logic_error
168 explicit length_error(const string& __arg);
169 #if __cplusplus >= 201103L
170 explicit length_error(const char*);
172 virtual ~length_error() _GLIBCXX_USE_NOEXCEPT;
175 /** This represents an argument whose value is not within the expected
176 * range (e.g., boundary checks in basic_string). */
177 class out_of_range : public logic_error
180 explicit out_of_range(const string& __arg);
181 #if __cplusplus >= 201103L
182 explicit out_of_range(const char*);
184 virtual ~out_of_range() _GLIBCXX_USE_NOEXCEPT;
187 /** Runtime errors represent problems outside the scope of a program;
188 * they cannot be easily predicted and can generally only be caught as
189 * the program executes.
190 * @brief One of two subclasses of exception.
192 class runtime_error : public exception
197 /** Takes a character string describing the error. */
199 runtime_error(const string& __arg);
201 #if __cplusplus >= 201103L
203 runtime_error(const char*);
206 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
207 runtime_error(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
208 runtime_error& operator=(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
211 virtual ~runtime_error() _GLIBCXX_USE_NOEXCEPT;
213 /** Returns a C-style character string describing the general cause of
214 * the current error (the same string passed to the ctor). */
216 what() const _GLIBCXX_USE_NOEXCEPT;
219 /** Thrown to indicate range errors in internal computations. */
220 class range_error : public runtime_error
223 explicit range_error(const string& __arg);
224 #if __cplusplus >= 201103L
225 explicit range_error(const char*);
227 virtual ~range_error() _GLIBCXX_USE_NOEXCEPT;
230 /** Thrown to indicate arithmetic overflow. */
231 class overflow_error : public runtime_error
234 explicit overflow_error(const string& __arg);
235 #if __cplusplus >= 201103L
236 explicit overflow_error(const char*);
238 virtual ~overflow_error() _GLIBCXX_USE_NOEXCEPT;
241 /** Thrown to indicate arithmetic underflow. */
242 class underflow_error : public runtime_error
245 explicit underflow_error(const string& __arg);
246 #if __cplusplus >= 201103L
247 explicit underflow_error(const char*);
249 virtual ~underflow_error() _GLIBCXX_USE_NOEXCEPT;
252 // @} group exceptions
254 _GLIBCXX_END_NAMESPACE_VERSION
257 #endif /* _GLIBCXX_STDEXCEPT */