stdexcept 7.6 KB

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