stdexcept 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <__assert> // all public C++ headers provide the assertion handler
  35. #include <__config>
  36. #include <__exception/exception.h>
  37. #include <__verbose_abort>
  38. #include <iosfwd> // for string forward decl
  39. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  40. # pragma GCC system_header
  41. #endif
  42. _LIBCPP_BEGIN_NAMESPACE_STD
  43. #ifndef _LIBCPP_ABI_VCRUNTIME
  44. class _LIBCPP_HIDDEN __libcpp_refstring
  45. {
  46. const char* __imp_;
  47. bool __uses_refcount() const;
  48. public:
  49. explicit __libcpp_refstring(const char* __msg);
  50. __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
  51. __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
  52. ~__libcpp_refstring();
  53. _LIBCPP_HIDE_FROM_ABI const char* c_str() const _NOEXCEPT {return __imp_;}
  54. };
  55. #endif // !_LIBCPP_ABI_VCRUNTIME
  56. _LIBCPP_END_NAMESPACE_STD
  57. namespace std // purposefully not using versioning namespace
  58. {
  59. class _LIBCPP_EXPORTED_FROM_ABI logic_error
  60. : public exception
  61. {
  62. #ifndef _LIBCPP_ABI_VCRUNTIME
  63. private:
  64. _VSTD::__libcpp_refstring __imp_;
  65. public:
  66. explicit logic_error(const string&);
  67. explicit logic_error(const char*);
  68. logic_error(const logic_error&) _NOEXCEPT;
  69. logic_error& operator=(const logic_error&) _NOEXCEPT;
  70. ~logic_error() _NOEXCEPT override;
  71. const char* what() const _NOEXCEPT override;
  72. #else
  73. public:
  74. explicit logic_error(const _VSTD::string&); // Symbol uses versioned std::string
  75. _LIBCPP_INLINE_VISIBILITY explicit logic_error(const char* __s) : exception(__s) {}
  76. #endif
  77. };
  78. class _LIBCPP_EXPORTED_FROM_ABI runtime_error
  79. : public exception
  80. {
  81. #ifndef _LIBCPP_ABI_VCRUNTIME
  82. private:
  83. _VSTD::__libcpp_refstring __imp_;
  84. public:
  85. explicit runtime_error(const string&);
  86. explicit runtime_error(const char*);
  87. runtime_error(const runtime_error&) _NOEXCEPT;
  88. runtime_error& operator=(const runtime_error&) _NOEXCEPT;
  89. ~runtime_error() _NOEXCEPT override;
  90. const char* what() const _NOEXCEPT override;
  91. #else
  92. public:
  93. explicit runtime_error(const _VSTD::string&); // Symbol uses versioned std::string
  94. _LIBCPP_INLINE_VISIBILITY explicit runtime_error(const char* __s) : exception(__s) {}
  95. #endif // _LIBCPP_ABI_VCRUNTIME
  96. };
  97. class _LIBCPP_EXPORTED_FROM_ABI domain_error
  98. : public logic_error
  99. {
  100. public:
  101. _LIBCPP_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {}
  102. _LIBCPP_INLINE_VISIBILITY explicit domain_error(const char* __s) : logic_error(__s) {}
  103. #ifndef _LIBCPP_ABI_VCRUNTIME
  104. _LIBCPP_HIDE_FROM_ABI domain_error(const domain_error&) _NOEXCEPT = default;
  105. _LIBCPP_HIDE_FROM_ABI domain_error& operator=(const domain_error&) _NOEXCEPT = default;
  106. ~domain_error() _NOEXCEPT override;
  107. #endif
  108. };
  109. class _LIBCPP_EXPORTED_FROM_ABI invalid_argument
  110. : public logic_error
  111. {
  112. public:
  113. _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
  114. _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s) : logic_error(__s) {}
  115. #ifndef _LIBCPP_ABI_VCRUNTIME
  116. _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT = default;
  117. _LIBCPP_HIDE_FROM_ABI invalid_argument& operator=(const invalid_argument&) _NOEXCEPT = default;
  118. ~invalid_argument() _NOEXCEPT override;
  119. #endif
  120. };
  121. class _LIBCPP_EXPORTED_FROM_ABI length_error
  122. : public logic_error
  123. {
  124. public:
  125. _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
  126. _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s) : logic_error(__s) {}
  127. #ifndef _LIBCPP_ABI_VCRUNTIME
  128. _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT = default;
  129. _LIBCPP_HIDE_FROM_ABI length_error& operator=(const length_error&) _NOEXCEPT = default;
  130. ~length_error() _NOEXCEPT override;
  131. #endif
  132. };
  133. class _LIBCPP_EXPORTED_FROM_ABI out_of_range
  134. : public logic_error
  135. {
  136. public:
  137. _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
  138. _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s) : logic_error(__s) {}
  139. #ifndef _LIBCPP_ABI_VCRUNTIME
  140. _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT = default;
  141. _LIBCPP_HIDE_FROM_ABI out_of_range& operator=(const out_of_range&) _NOEXCEPT = default;
  142. ~out_of_range() _NOEXCEPT override;
  143. #endif
  144. };
  145. class _LIBCPP_EXPORTED_FROM_ABI range_error
  146. : public runtime_error
  147. {
  148. public:
  149. _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
  150. _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s) : runtime_error(__s) {}
  151. #ifndef _LIBCPP_ABI_VCRUNTIME
  152. _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT = default;
  153. _LIBCPP_HIDE_FROM_ABI range_error& operator=(const range_error&) _NOEXCEPT = default;
  154. ~range_error() _NOEXCEPT override;
  155. #endif
  156. };
  157. class _LIBCPP_EXPORTED_FROM_ABI overflow_error
  158. : public runtime_error
  159. {
  160. public:
  161. _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
  162. _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s) : runtime_error(__s) {}
  163. #ifndef _LIBCPP_ABI_VCRUNTIME
  164. _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT = default;
  165. _LIBCPP_HIDE_FROM_ABI overflow_error& operator=(const overflow_error&) _NOEXCEPT = default;
  166. ~overflow_error() _NOEXCEPT override;
  167. #endif
  168. };
  169. class _LIBCPP_EXPORTED_FROM_ABI underflow_error
  170. : public runtime_error
  171. {
  172. public:
  173. _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
  174. _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s) : runtime_error(__s) {}
  175. #ifndef _LIBCPP_ABI_VCRUNTIME
  176. _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT = default;
  177. _LIBCPP_HIDE_FROM_ABI underflow_error& operator=(const underflow_error&) _NOEXCEPT = default;
  178. ~underflow_error() _NOEXCEPT override;
  179. #endif
  180. };
  181. } // namespace std
  182. _LIBCPP_BEGIN_NAMESPACE_STD
  183. // in the dylib
  184. _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
  185. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  186. void __throw_logic_error(const char*__msg)
  187. {
  188. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  189. throw logic_error(__msg);
  190. #else
  191. _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  192. #endif
  193. }
  194. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  195. void __throw_domain_error(const char*__msg)
  196. {
  197. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  198. throw domain_error(__msg);
  199. #else
  200. _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  201. #endif
  202. }
  203. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  204. void __throw_invalid_argument(const char*__msg)
  205. {
  206. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  207. throw invalid_argument(__msg);
  208. #else
  209. _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  210. #endif
  211. }
  212. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  213. void __throw_length_error(const char*__msg)
  214. {
  215. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  216. throw length_error(__msg);
  217. #else
  218. _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  219. #endif
  220. }
  221. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  222. void __throw_out_of_range(const char*__msg)
  223. {
  224. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  225. throw out_of_range(__msg);
  226. #else
  227. _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  228. #endif
  229. }
  230. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  231. void __throw_range_error(const char*__msg)
  232. {
  233. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  234. throw range_error(__msg);
  235. #else
  236. _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  237. #endif
  238. }
  239. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  240. void __throw_overflow_error(const char*__msg)
  241. {
  242. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  243. throw overflow_error(__msg);
  244. #else
  245. _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  246. #endif
  247. }
  248. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  249. void __throw_underflow_error(const char*__msg)
  250. {
  251. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  252. throw underflow_error(__msg);
  253. #else
  254. _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
  255. #endif
  256. }
  257. _LIBCPP_END_NAMESPACE_STD
  258. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  259. # include <cstdlib>
  260. # include <exception>
  261. #endif
  262. #endif // _LIBCPP_STDEXCEPT