stdexcept 9.6 KB

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