stdexcept 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Standard exception classes -*- C++ -*-
  2. // Copyright (C) 2001-2015 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_USE_NOEXCEPT;
  47. __cow_string& operator=(const __cow_string&) _GLIBCXX_USE_NOEXCEPT;
  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_USE_NOEXCEPT;
  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);
  102. #if __cplusplus >= 201103L
  103. explicit
  104. logic_error(const char*);
  105. #endif
  106. #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
  107. logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
  108. logic_error& operator=(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
  109. #endif
  110. virtual ~logic_error() _GLIBCXX_USE_NOEXCEPT;
  111. /** Returns a C-style character string describing the general cause of
  112. * the current error (the same string passed to the ctor). */
  113. virtual const char*
  114. what() const _GLIBCXX_USE_NOEXCEPT;
  115. };
  116. /** Thrown by the library, or by you, to report domain errors (domain in
  117. * the mathematical sense). */
  118. class domain_error : public logic_error
  119. {
  120. public:
  121. explicit domain_error(const string& __arg);
  122. #if __cplusplus >= 201103L
  123. explicit domain_error(const char*);
  124. #endif
  125. virtual ~domain_error() _GLIBCXX_USE_NOEXCEPT;
  126. };
  127. /** Thrown to report invalid arguments to functions. */
  128. class invalid_argument : public logic_error
  129. {
  130. public:
  131. explicit invalid_argument(const string& __arg);
  132. #if __cplusplus >= 201103L
  133. explicit invalid_argument(const char*);
  134. #endif
  135. virtual ~invalid_argument() _GLIBCXX_USE_NOEXCEPT;
  136. };
  137. /** Thrown when an object is constructed that would exceed its maximum
  138. * permitted size (e.g., a basic_string instance). */
  139. class length_error : public logic_error
  140. {
  141. public:
  142. explicit length_error(const string& __arg);
  143. #if __cplusplus >= 201103L
  144. explicit length_error(const char*);
  145. #endif
  146. virtual ~length_error() _GLIBCXX_USE_NOEXCEPT;
  147. };
  148. /** This represents an argument whose value is not within the expected
  149. * range (e.g., boundary checks in basic_string). */
  150. class out_of_range : public logic_error
  151. {
  152. public:
  153. explicit out_of_range(const string& __arg);
  154. #if __cplusplus >= 201103L
  155. explicit out_of_range(const char*);
  156. #endif
  157. virtual ~out_of_range() _GLIBCXX_USE_NOEXCEPT;
  158. };
  159. /** Runtime errors represent problems outside the scope of a program;
  160. * they cannot be easily predicted and can generally only be caught as
  161. * the program executes.
  162. * @brief One of two subclasses of exception.
  163. */
  164. class runtime_error : public exception
  165. {
  166. __cow_string _M_msg;
  167. public:
  168. /** Takes a character string describing the error. */
  169. explicit
  170. runtime_error(const string& __arg);
  171. #if __cplusplus >= 201103L
  172. explicit
  173. runtime_error(const char*);
  174. #endif
  175. #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
  176. runtime_error(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
  177. runtime_error& operator=(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
  178. #endif
  179. virtual ~runtime_error() _GLIBCXX_USE_NOEXCEPT;
  180. /** Returns a C-style character string describing the general cause of
  181. * the current error (the same string passed to the ctor). */
  182. virtual const char*
  183. what() const _GLIBCXX_USE_NOEXCEPT;
  184. };
  185. /** Thrown to indicate range errors in internal computations. */
  186. class range_error : public runtime_error
  187. {
  188. public:
  189. explicit range_error(const string& __arg);
  190. #if __cplusplus >= 201103L
  191. explicit range_error(const char*);
  192. #endif
  193. virtual ~range_error() _GLIBCXX_USE_NOEXCEPT;
  194. };
  195. /** Thrown to indicate arithmetic overflow. */
  196. class overflow_error : public runtime_error
  197. {
  198. public:
  199. explicit overflow_error(const string& __arg);
  200. #if __cplusplus >= 201103L
  201. explicit overflow_error(const char*);
  202. #endif
  203. virtual ~overflow_error() _GLIBCXX_USE_NOEXCEPT;
  204. };
  205. /** Thrown to indicate arithmetic underflow. */
  206. class underflow_error : public runtime_error
  207. {
  208. public:
  209. explicit underflow_error(const string& __arg);
  210. #if __cplusplus >= 201103L
  211. explicit underflow_error(const char*);
  212. #endif
  213. virtual ~underflow_error() _GLIBCXX_USE_NOEXCEPT;
  214. };
  215. // @} group exceptions
  216. _GLIBCXX_END_NAMESPACE_VERSION
  217. } // namespace
  218. #endif /* _GLIBCXX_STDEXCEPT */