libstdc++
|
00001 // Standard exception classes -*- C++ -*- 00002 00003 // Copyright (C) 2001-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/stdexcept 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 // 00030 // ISO C++ 19.1 Exception classes 00031 // 00032 00033 #ifndef _GLIBCXX_STDEXCEPT 00034 #define _GLIBCXX_STDEXCEPT 1 00035 00036 #pragma GCC system_header 00037 00038 #include <exception> 00039 #include <string> 00040 00041 namespace std _GLIBCXX_VISIBILITY(default) 00042 { 00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00044 00045 #if _GLIBCXX_USE_DUAL_ABI 00046 #if _GLIBCXX_USE_CXX11_ABI 00047 // Emulates an old COW string when the new std::string is in use. 00048 struct __cow_string 00049 { 00050 union { 00051 const char* _M_p; 00052 char _M_bytes[sizeof(const char*)]; 00053 }; 00054 00055 __cow_string(); 00056 __cow_string(const std::string&); 00057 __cow_string(const char*, size_t); 00058 __cow_string(const __cow_string&) _GLIBCXX_USE_NOEXCEPT; 00059 __cow_string& operator=(const __cow_string&) _GLIBCXX_USE_NOEXCEPT; 00060 ~__cow_string(); 00061 #if __cplusplus >= 201103L 00062 __cow_string(__cow_string&&) noexcept; 00063 __cow_string& operator=(__cow_string&&) noexcept; 00064 #endif 00065 }; 00066 00067 typedef basic_string<char> __sso_string; 00068 #else // _GLIBCXX_USE_CXX11_ABI 00069 typedef basic_string<char> __cow_string; 00070 00071 // Emulates a new SSO string when the old std::string is in use. 00072 struct __sso_string 00073 { 00074 struct __str 00075 { 00076 const char* _M_p; 00077 size_t _M_string_length; 00078 char _M_local_buf[16]; 00079 }; 00080 00081 union { 00082 __str _M_s; 00083 char _M_bytes[sizeof(__str)]; 00084 }; 00085 00086 __sso_string() _GLIBCXX_USE_NOEXCEPT; 00087 __sso_string(const std::string&); 00088 __sso_string(const char*, size_t); 00089 __sso_string(const __sso_string&); 00090 __sso_string& operator=(const __sso_string&); 00091 ~__sso_string(); 00092 #if __cplusplus >= 201103L 00093 __sso_string(__sso_string&&) noexcept; 00094 __sso_string& operator=(__sso_string&&) noexcept; 00095 #endif 00096 }; 00097 #endif // _GLIBCXX_USE_CXX11_ABI 00098 #else // _GLIBCXX_USE_DUAL_ABI 00099 typedef basic_string<char> __sso_string; 00100 typedef basic_string<char> __cow_string; 00101 #endif 00102 00103 /** 00104 * @addtogroup exceptions 00105 * @{ 00106 */ 00107 00108 /** Logic errors represent problems in the internal logic of a program; 00109 * in theory, these are preventable, and even detectable before the 00110 * program runs (e.g., violations of class invariants). 00111 * @brief One of two subclasses of exception. 00112 */ 00113 class logic_error : public exception 00114 { 00115 __cow_string _M_msg; 00116 00117 public: 00118 /** Takes a character string describing the error. */ 00119 explicit 00120 logic_error(const string& __arg) _GLIBCXX_TXN_SAFE; 00121 00122 #if __cplusplus >= 201103L 00123 explicit 00124 logic_error(const char*) _GLIBCXX_TXN_SAFE; 00125 #endif 00126 00127 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS 00128 logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT; 00129 logic_error& operator=(const logic_error&) _GLIBCXX_USE_NOEXCEPT; 00130 #endif 00131 00132 virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 00133 00134 /** Returns a C-style character string describing the general cause of 00135 * the current error (the same string passed to the ctor). */ 00136 virtual const char* 00137 what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 00138 00139 # ifdef _GLIBCXX_TM_TS_INTERNAL 00140 friend void* 00141 ::_txnal_logic_error_get_msg(void* e); 00142 # endif 00143 }; 00144 00145 /** Thrown by the library, or by you, to report domain errors (domain in 00146 * the mathematical sense). */ 00147 class domain_error : public logic_error 00148 { 00149 public: 00150 explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE; 00151 #if __cplusplus >= 201103L 00152 explicit domain_error(const char*) _GLIBCXX_TXN_SAFE; 00153 #endif 00154 virtual ~domain_error() _GLIBCXX_USE_NOEXCEPT; 00155 }; 00156 00157 /** Thrown to report invalid arguments to functions. */ 00158 class invalid_argument : public logic_error 00159 { 00160 public: 00161 explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE; 00162 #if __cplusplus >= 201103L 00163 explicit invalid_argument(const char*) _GLIBCXX_TXN_SAFE; 00164 #endif 00165 virtual ~invalid_argument() _GLIBCXX_USE_NOEXCEPT; 00166 }; 00167 00168 /** Thrown when an object is constructed that would exceed its maximum 00169 * permitted size (e.g., a basic_string instance). */ 00170 class length_error : public logic_error 00171 { 00172 public: 00173 explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE; 00174 #if __cplusplus >= 201103L 00175 explicit length_error(const char*) _GLIBCXX_TXN_SAFE; 00176 #endif 00177 virtual ~length_error() _GLIBCXX_USE_NOEXCEPT; 00178 }; 00179 00180 /** This represents an argument whose value is not within the expected 00181 * range (e.g., boundary checks in basic_string). */ 00182 class out_of_range : public logic_error 00183 { 00184 public: 00185 explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE; 00186 #if __cplusplus >= 201103L 00187 explicit out_of_range(const char*) _GLIBCXX_TXN_SAFE; 00188 #endif 00189 virtual ~out_of_range() _GLIBCXX_USE_NOEXCEPT; 00190 }; 00191 00192 /** Runtime errors represent problems outside the scope of a program; 00193 * they cannot be easily predicted and can generally only be caught as 00194 * the program executes. 00195 * @brief One of two subclasses of exception. 00196 */ 00197 class runtime_error : public exception 00198 { 00199 __cow_string _M_msg; 00200 00201 public: 00202 /** Takes a character string describing the error. */ 00203 explicit 00204 runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE; 00205 00206 #if __cplusplus >= 201103L 00207 explicit 00208 runtime_error(const char*) _GLIBCXX_TXN_SAFE; 00209 #endif 00210 00211 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS 00212 runtime_error(const runtime_error&) _GLIBCXX_USE_NOEXCEPT; 00213 runtime_error& operator=(const runtime_error&) _GLIBCXX_USE_NOEXCEPT; 00214 #endif 00215 00216 virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 00217 00218 /** Returns a C-style character string describing the general cause of 00219 * the current error (the same string passed to the ctor). */ 00220 virtual const char* 00221 what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT; 00222 00223 # ifdef _GLIBCXX_TM_TS_INTERNAL 00224 friend void* 00225 ::_txnal_runtime_error_get_msg(void* e); 00226 # endif 00227 }; 00228 00229 /** Thrown to indicate range errors in internal computations. */ 00230 class range_error : public runtime_error 00231 { 00232 public: 00233 explicit range_error(const string& __arg) _GLIBCXX_TXN_SAFE; 00234 #if __cplusplus >= 201103L 00235 explicit range_error(const char*) _GLIBCXX_TXN_SAFE; 00236 #endif 00237 virtual ~range_error() _GLIBCXX_USE_NOEXCEPT; 00238 }; 00239 00240 /** Thrown to indicate arithmetic overflow. */ 00241 class overflow_error : public runtime_error 00242 { 00243 public: 00244 explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE; 00245 #if __cplusplus >= 201103L 00246 explicit overflow_error(const char*) _GLIBCXX_TXN_SAFE; 00247 #endif 00248 virtual ~overflow_error() _GLIBCXX_USE_NOEXCEPT; 00249 }; 00250 00251 /** Thrown to indicate arithmetic underflow. */ 00252 class underflow_error : public runtime_error 00253 { 00254 public: 00255 explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE; 00256 #if __cplusplus >= 201103L 00257 explicit underflow_error(const char*) _GLIBCXX_TXN_SAFE; 00258 #endif 00259 virtual ~underflow_error() _GLIBCXX_USE_NOEXCEPT; 00260 }; 00261 00262 // @} group exceptions 00263 00264 _GLIBCXX_END_NAMESPACE_VERSION 00265 } // namespace 00266 00267 #endif /* _GLIBCXX_STDEXCEPT */