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