stdexcept 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_STDEXCEPT
  10. #define _LIBCPP_STDEXCEPT
  11. /*
  12. stdexcept synopsis
  13. namespace std
  14. {
  15. class logic_error;
  16. class domain_error;
  17. class invalid_argument;
  18. class length_error;
  19. class out_of_range;
  20. class runtime_error;
  21. class range_error;
  22. class overflow_error;
  23. class underflow_error;
  24. for each class xxx_error:
  25. class xxx_error : public exception // at least indirectly
  26. {
  27. public:
  28. explicit xxx_error(const string& what_arg);
  29. explicit xxx_error(const char* what_arg);
  30. virtual const char* what() const noexcept // returns what_arg
  31. };
  32. } // std
  33. */
  34. #include <__config>
  35. #include <__exception/exception.h>
  36. #include <__fwd/string.h>
  37. #include <__verbose_abort>
  38. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  39. # pragma GCC system_header
  40. #endif
  41. _LIBCPP_BEGIN_NAMESPACE_STD
  42. #ifndef _LIBCPP_ABI_VCRUNTIME
  43. class _LIBCPP_HIDDEN __libcpp_refstring {
  44. const char* __imp_;
  45. bool __uses_refcount() const;
  46. public:
  47. explicit __libcpp_refstring(const char* __msg);
  48. __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
  49. __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
  50. ~__libcpp_refstring();
  51. _LIBCPP_HIDE_FROM_ABI const char* c_str() const _NOEXCEPT { return __imp_; }
  52. };
  53. #endif // !_LIBCPP_ABI_VCRUNTIME
  54. _LIBCPP_END_NAMESPACE_STD
  55. namespace std // purposefully not using versioning namespace
  56. {
  57. class _LIBCPP_EXPORTED_FROM_ABI logic_error : public exception {
  58. #ifndef _LIBCPP_ABI_VCRUNTIME
  59. private:
  60. std::__libcpp_refstring __imp_;
  61. public:
  62. explicit logic_error(const string&);
  63. explicit logic_error(const char*);
  64. logic_error(const logic_error&) _NOEXCEPT;
  65. logic_error& operator=(const logic_error&) _NOEXCEPT;
  66. ~logic_error() _NOEXCEPT override;
  67. const char* what() const _NOEXCEPT override;
  68. #else
  69. public:
  70. explicit logic_error(const std::string&); // Symbol uses versioned std::string
  71. _LIBCPP_HIDE_FROM_ABI explicit logic_error(const char* __s) : exception(__s) {}
  72. #endif
  73. };
  74. class _LIBCPP_EXPORTED_FROM_ABI runtime_error : public exception {
  75. #ifndef _LIBCPP_ABI_VCRUNTIME
  76. private:
  77. std::__libcpp_refstring __imp_;
  78. public:
  79. explicit runtime_error(const string&);
  80. explicit runtime_error(const char*);
  81. runtime_error(const runtime_error&) _NOEXCEPT;
  82. runtime_error& operator=(const runtime_error&) _NOEXCEPT;
  83. ~runtime_error() _NOEXCEPT override;
  84. const char* what() const _NOEXCEPT override;
  85. #else
  86. public:
  87. explicit runtime_error(const std::string&); // Symbol uses versioned std::string
  88. _LIBCPP_HIDE_FROM_ABI explicit runtime_error(const char* __s) : exception(__s) {}
  89. #endif // _LIBCPP_ABI_VCRUNTIME
  90. };
  91. class _LIBCPP_EXPORTED_FROM_ABI domain_error : public logic_error {
  92. public:
  93. _LIBCPP_HIDE_FROM_ABI explicit domain_error(const string& __s) : logic_error(__s) {}
  94. _LIBCPP_HIDE_FROM_ABI explicit domain_error(const char* __s) : logic_error(__s) {}
  95. #ifndef _LIBCPP_ABI_VCRUNTIME
  96. _LIBCPP_HIDE_FROM_ABI domain_error(const domain_error&) _NOEXCEPT = default;
  97. _LIBCPP_HIDE_FROM_ABI domain_error& operator=(const domain_error&) _NOEXCEPT = default;
  98. ~domain_error() _NOEXCEPT override;
  99. #endif
  100. };
  101. class _LIBCPP_EXPORTED_FROM_ABI invalid_argument : public logic_error {
  102. public:
  103. _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const string& __s) : logic_error(__s) {}
  104. _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const char* __s) : logic_error(__s) {}
  105. #ifndef _LIBCPP_ABI_VCRUNTIME
  106. _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT = default;
  107. _LIBCPP_HIDE_FROM_ABI invalid_argument& operator=(const invalid_argument&) _NOEXCEPT = default;
  108. ~invalid_argument() _NOEXCEPT override;
  109. #endif
  110. };
  111. class _LIBCPP_EXPORTED_FROM_ABI length_error : public logic_error {
  112. public:
  113. _LIBCPP_HIDE_FROM_ABI explicit length_error(const string& __s) : logic_error(__s) {}
  114. _LIBCPP_HIDE_FROM_ABI explicit length_error(const char* __s) : logic_error(__s) {}
  115. #ifndef _LIBCPP_ABI_VCRUNTIME
  116. _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT = default;
  117. _LIBCPP_HIDE_FROM_ABI length_error& operator=(const length_error&) _NOEXCEPT = default;
  118. ~length_error() _NOEXCEPT override;
  119. #endif
  120. };
  121. class _LIBCPP_EXPORTED_FROM_ABI out_of_range : public logic_error {
  122. public:
  123. _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const string& __s) : logic_error(__s) {}
  124. _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const char* __s) : logic_error(__s) {}
  125. #ifndef _LIBCPP_ABI_VCRUNTIME
  126. _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT = default;
  127. _LIBCPP_HIDE_FROM_ABI out_of_range& operator=(const out_of_range&) _NOEXCEPT = default;
  128. ~out_of_range() _NOEXCEPT override;
  129. #endif
  130. };
  131. class _LIBCPP_EXPORTED_FROM_ABI range_error : public runtime_error {
  132. public:
  133. _LIBCPP_HIDE_FROM_ABI explicit range_error(const string& __s) : runtime_error(__s) {}
  134. _LIBCPP_HIDE_FROM_ABI explicit range_error(const char* __s) : runtime_error(__s) {}
  135. #ifndef _LIBCPP_ABI_VCRUNTIME
  136. _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT = default;
  137. _LIBCPP_HIDE_FROM_ABI range_error& operator=(const range_error&) _NOEXCEPT = default;
  138. ~range_error() _NOEXCEPT override;
  139. #endif
  140. };
  141. class _LIBCPP_EXPORTED_FROM_ABI overflow_error : public runtime_error {
  142. public:
  143. _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const string& __s) : runtime_error(__s) {}
  144. _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const char* __s) : runtime_error(__s) {}
  145. #ifndef _LIBCPP_ABI_VCRUNTIME
  146. _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT = default;
  147. _LIBCPP_HIDE_FROM_ABI overflow_error& operator=(const overflow_error&) _NOEXCEPT = default;
  148. ~overflow_error() _NOEXCEPT override;
  149. #endif
  150. };
  151. class _LIBCPP_EXPORTED_FROM_ABI underflow_error : public runtime_error {
  152. public:
  153. _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const string& __s) : runtime_error(__s) {}
  154. _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const char* __s) : runtime_error(__s) {}
  155. #ifndef _LIBCPP_ABI_VCRUNTIME
  156. _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT = default;
  157. _LIBCPP_HIDE_FROM_ABI underflow_error& operator=(const underflow_error&) _NOEXCEPT = default;
  158. ~underflow_error() _NOEXCEPT override;
  159. #endif
  160. };
  161. } // namespace std
  162. _LIBCPP_BEGIN_NAMESPACE_STD
  163. // in the dylib
  164. _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
  165. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_logic_error(const char* __msg) {
  166. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  167. throw logic_error(__msg);
  168. #else
  169. _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  170. #endif
  171. }
  172. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_domain_error(const char* __msg) {
  173. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  174. throw domain_error(__msg);
  175. #else
  176. _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  177. #endif
  178. }
  179. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_invalid_argument(const char* __msg) {
  180. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  181. throw invalid_argument(__msg);
  182. #else
  183. _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  184. #endif
  185. }
  186. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_length_error(const char* __msg) {
  187. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  188. throw length_error(__msg);
  189. #else
  190. _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  191. #endif
  192. }
  193. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range(const char* __msg) {
  194. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  195. throw out_of_range(__msg);
  196. #else
  197. _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  198. #endif
  199. }
  200. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_range_error(const char* __msg) {
  201. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  202. throw range_error(__msg);
  203. #else
  204. _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  205. #endif
  206. }
  207. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_overflow_error(const char* __msg) {
  208. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  209. throw overflow_error(__msg);
  210. #else
  211. _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  212. #endif
  213. }
  214. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_underflow_error(const char* __msg) {
  215. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  216. throw underflow_error(__msg);
  217. #else
  218. _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  219. #endif
  220. }
  221. _LIBCPP_END_NAMESPACE_STD
  222. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  223. # include <cstdlib>
  224. # include <exception>
  225. # include <iosfwd>
  226. #endif
  227. #endif // _LIBCPP_STDEXCEPT