stdexcept 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <cstdlib>
  37. #include <exception>
  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. 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_EXCEPTION_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_EXCEPTION_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_EXCEPTION_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. domain_error(const domain_error&) _NOEXCEPT = default;
  105. ~domain_error() _NOEXCEPT override;
  106. #endif
  107. };
  108. class _LIBCPP_EXCEPTION_ABI invalid_argument
  109. : public logic_error
  110. {
  111. public:
  112. _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
  113. _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s) : logic_error(__s) {}
  114. #ifndef _LIBCPP_ABI_VCRUNTIME
  115. invalid_argument(const invalid_argument&) _NOEXCEPT = default;
  116. ~invalid_argument() _NOEXCEPT override;
  117. #endif
  118. };
  119. class _LIBCPP_EXCEPTION_ABI length_error
  120. : public logic_error
  121. {
  122. public:
  123. _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
  124. _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s) : logic_error(__s) {}
  125. #ifndef _LIBCPP_ABI_VCRUNTIME
  126. length_error(const length_error&) _NOEXCEPT = default;
  127. ~length_error() _NOEXCEPT override;
  128. #endif
  129. };
  130. class _LIBCPP_EXCEPTION_ABI out_of_range
  131. : public logic_error
  132. {
  133. public:
  134. _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
  135. _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s) : logic_error(__s) {}
  136. #ifndef _LIBCPP_ABI_VCRUNTIME
  137. out_of_range(const out_of_range&) _NOEXCEPT = default;
  138. ~out_of_range() _NOEXCEPT override;
  139. #endif
  140. };
  141. class _LIBCPP_EXCEPTION_ABI range_error
  142. : public runtime_error
  143. {
  144. public:
  145. _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
  146. _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s) : runtime_error(__s) {}
  147. #ifndef _LIBCPP_ABI_VCRUNTIME
  148. range_error(const range_error&) _NOEXCEPT = default;
  149. ~range_error() _NOEXCEPT override;
  150. #endif
  151. };
  152. class _LIBCPP_EXCEPTION_ABI overflow_error
  153. : public runtime_error
  154. {
  155. public:
  156. _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
  157. _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s) : runtime_error(__s) {}
  158. #ifndef _LIBCPP_ABI_VCRUNTIME
  159. overflow_error(const overflow_error&) _NOEXCEPT = default;
  160. ~overflow_error() _NOEXCEPT override;
  161. #endif
  162. };
  163. class _LIBCPP_EXCEPTION_ABI underflow_error
  164. : public runtime_error
  165. {
  166. public:
  167. _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
  168. _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s) : runtime_error(__s) {}
  169. #ifndef _LIBCPP_ABI_VCRUNTIME
  170. underflow_error(const underflow_error&) _NOEXCEPT = default;
  171. ~underflow_error() _NOEXCEPT override;
  172. #endif
  173. };
  174. } // namespace std
  175. _LIBCPP_BEGIN_NAMESPACE_STD
  176. // in the dylib
  177. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
  178. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  179. void __throw_logic_error(const char*__msg)
  180. {
  181. #ifndef _LIBCPP_NO_EXCEPTIONS
  182. throw logic_error(__msg);
  183. #else
  184. ((void)__msg);
  185. _VSTD::abort();
  186. #endif
  187. }
  188. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  189. void __throw_domain_error(const char*__msg)
  190. {
  191. #ifndef _LIBCPP_NO_EXCEPTIONS
  192. throw domain_error(__msg);
  193. #else
  194. ((void)__msg);
  195. _VSTD::abort();
  196. #endif
  197. }
  198. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  199. void __throw_invalid_argument(const char*__msg)
  200. {
  201. #ifndef _LIBCPP_NO_EXCEPTIONS
  202. throw invalid_argument(__msg);
  203. #else
  204. ((void)__msg);
  205. _VSTD::abort();
  206. #endif
  207. }
  208. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  209. void __throw_length_error(const char*__msg)
  210. {
  211. #ifndef _LIBCPP_NO_EXCEPTIONS
  212. throw length_error(__msg);
  213. #else
  214. ((void)__msg);
  215. _VSTD::abort();
  216. #endif
  217. }
  218. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  219. void __throw_out_of_range(const char*__msg)
  220. {
  221. #ifndef _LIBCPP_NO_EXCEPTIONS
  222. throw out_of_range(__msg);
  223. #else
  224. ((void)__msg);
  225. _VSTD::abort();
  226. #endif
  227. }
  228. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  229. void __throw_range_error(const char*__msg)
  230. {
  231. #ifndef _LIBCPP_NO_EXCEPTIONS
  232. throw range_error(__msg);
  233. #else
  234. ((void)__msg);
  235. _VSTD::abort();
  236. #endif
  237. }
  238. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  239. void __throw_overflow_error(const char*__msg)
  240. {
  241. #ifndef _LIBCPP_NO_EXCEPTIONS
  242. throw overflow_error(__msg);
  243. #else
  244. ((void)__msg);
  245. _VSTD::abort();
  246. #endif
  247. }
  248. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  249. void __throw_underflow_error(const char*__msg)
  250. {
  251. #ifndef _LIBCPP_NO_EXCEPTIONS
  252. throw underflow_error(__msg);
  253. #else
  254. ((void)__msg);
  255. _VSTD::abort();
  256. #endif
  257. }
  258. _LIBCPP_END_NAMESPACE_STD
  259. #endif // _LIBCPP_STDEXCEPT