1// Standard exception classes -*- C++ -*- 2 3// Copyright (C) 2001-2017 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#pragma GCC system_header 37 38#include <exception> 39#include <string> 40 41namespace std _GLIBCXX_VISIBILITY(default) 42{ 43_GLIBCXX_BEGIN_NAMESPACE_VERSION 44 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. 48 struct __cow_string 49 { 50 union { 51 const char* _M_p; 52 char _M_bytes[sizeof(const char*)]; 53 }; 54 55 __cow_string(); 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; 60 ~__cow_string(); 61#if __cplusplus >= 201103L 62 __cow_string(__cow_string&&) noexcept; 63 __cow_string& operator=(__cow_string&&) noexcept; 64#endif 65 }; 66 67 typedef basic_string<char> __sso_string; 68#else // _GLIBCXX_USE_CXX11_ABI 69 typedef basic_string<char> __cow_string; 70 71 // Emulates a new SSO string when the old std::string is in use. 72 struct __sso_string 73 { 74 struct __str 75 { 76 const char* _M_p; 77 size_t _M_string_length; 78 char _M_local_buf[16]; 79 }; 80 81 union { 82 __str _M_s; 83 char _M_bytes[sizeof(__str)]; 84 }; 85 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&); 91 ~__sso_string(); 92#if __cplusplus >= 201103L 93 __sso_string(__sso_string&&) noexcept; 94 __sso_string& operator=(__sso_string&&) noexcept; 95#endif 96 }; 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; 101#endif 102 103 /** 104 * @addtogroup exceptions 105 * @{ 106 */ 107 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. 112 */ 113 class logic_error : public exception 114 { 115 __cow_string _M_msg; 116 117 public: 118 /** Takes a character string describing the error. */ 119 explicit 120 logic_error(const string& __arg) _GLIBCXX_TXN_SAFE; 121 122#if __cplusplus >= 201103L 123 explicit 124 logic_error(const char*) _GLIBCXX_TXN_SAFE; 125#endif 126 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; 130#endif 131 132 virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 133 134 /** Returns a C-style character string describing the general cause of 135 * the current error (the same string passed to the ctor). */ 136 virtual const char* 137 what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 138 139# ifdef _GLIBCXX_TM_TS_INTERNAL 140 friend void* 141 ::_txnal_logic_error_get_msg(void* e); 142# endif 143 }; 144 145 /** Thrown by the library, or by you, to report domain errors (domain in 146 * the mathematical sense). */ 147 class domain_error : public logic_error 148 { 149 public: 150 explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE; 151#if __cplusplus >= 201103L 152 explicit domain_error(const char*) _GLIBCXX_TXN_SAFE; 153#endif 154 virtual ~domain_error() _GLIBCXX_USE_NOEXCEPT; 155 }; 156 157 /** Thrown to report invalid arguments to functions. */ 158 class invalid_argument : public logic_error 159 { 160 public: 161 explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE; 162#if __cplusplus >= 201103L 163 explicit invalid_argument(const char*) _GLIBCXX_TXN_SAFE; 164#endif 165 virtual ~invalid_argument() _GLIBCXX_USE_NOEXCEPT; 166 }; 167 168 /** Thrown when an object is constructed that would exceed its maximum 169 * permitted size (e.g., a basic_string instance). */ 170 class length_error : public logic_error 171 { 172 public: 173 explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE; 174#if __cplusplus >= 201103L 175 explicit length_error(const char*) _GLIBCXX_TXN_SAFE; 176#endif 177 virtual ~length_error() _GLIBCXX_USE_NOEXCEPT; 178 }; 179 180 /** This represents an argument whose value is not within the expected 181 * range (e.g., boundary checks in basic_string). */ 182 class out_of_range : public logic_error 183 { 184 public: 185 explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE; 186#if __cplusplus >= 201103L 187 explicit out_of_range(const char*) _GLIBCXX_TXN_SAFE; 188#endif 189 virtual ~out_of_range() _GLIBCXX_USE_NOEXCEPT; 190 }; 191 192 /** Runtime errors represent problems outside the scope of a program; 193 * they cannot be easily predicted and can generally only be caught as 194 * the program executes. 195 * @brief One of two subclasses of exception. 196 */ 197 class runtime_error : public exception 198 { 199 __cow_string _M_msg; 200 201 public: 202 /** Takes a character string describing the error. */ 203 explicit 204 runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE; 205 206#if __cplusplus >= 201103L 207 explicit 208 runtime_error(const char*) _GLIBCXX_TXN_SAFE; 209#endif 210 211#if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS 212 runtime_error(const runtime_error&) _GLIBCXX_USE_NOEXCEPT; 213 runtime_error& operator=(const runtime_error&) _GLIBCXX_USE_NOEXCEPT; 214#endif 215 216 virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 217 218 /** Returns a C-style character string describing the general cause of 219 * the current error (the same string passed to the ctor). */ 220 virtual const char* 221 what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 222 223# ifdef _GLIBCXX_TM_TS_INTERNAL 224 friend void* 225 ::_txnal_runtime_error_get_msg(void* e); 226# endif 227 }; 228 229 /** Thrown to indicate range errors in internal computations. */ 230 class range_error : public runtime_error 231 { 232 public: 233 explicit range_error(const string& __arg) _GLIBCXX_TXN_SAFE; 234#if __cplusplus >= 201103L 235 explicit range_error(const char*) _GLIBCXX_TXN_SAFE; 236#endif 237 virtual ~range_error() _GLIBCXX_USE_NOEXCEPT; 238 }; 239 240 /** Thrown to indicate arithmetic overflow. */ 241 class overflow_error : public runtime_error 242 { 243 public: 244 explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE; 245#if __cplusplus >= 201103L 246 explicit overflow_error(const char*) _GLIBCXX_TXN_SAFE; 247#endif 248 virtual ~overflow_error() _GLIBCXX_USE_NOEXCEPT; 249 }; 250 251 /** Thrown to indicate arithmetic underflow. */ 252 class underflow_error : public runtime_error 253 { 254 public: 255 explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE; 256#if __cplusplus >= 201103L 257 explicit underflow_error(const char*) _GLIBCXX_TXN_SAFE; 258#endif 259 virtual ~underflow_error() _GLIBCXX_USE_NOEXCEPT; 260 }; 261 262 // @} group exceptions 263 264_GLIBCXX_END_NAMESPACE_VERSION 265} // namespace 266 267#endif /* _GLIBCXX_STDEXCEPT */ 268