stdexcept 7.2 KB

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