xref: /freebsd/contrib/llvm-project/libcxx/include/stdexcept (revision 700637cbb5e582861067a11aaca4d053546871d2)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_STDEXCEPT
110b57cec5SDimitry Andric#define _LIBCPP_STDEXCEPT
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    stdexcept synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andricclass logic_error;
200b57cec5SDimitry Andricclass domain_error;
210b57cec5SDimitry Andricclass invalid_argument;
220b57cec5SDimitry Andricclass length_error;
230b57cec5SDimitry Andricclass out_of_range;
240b57cec5SDimitry Andricclass runtime_error;
250b57cec5SDimitry Andricclass range_error;
260b57cec5SDimitry Andricclass overflow_error;
270b57cec5SDimitry Andricclass underflow_error;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andricfor each class xxx_error:
300b57cec5SDimitry Andric
310b57cec5SDimitry Andricclass xxx_error : public exception // at least indirectly
320b57cec5SDimitry Andric{
330b57cec5SDimitry Andricpublic:
340b57cec5SDimitry Andric    explicit xxx_error(const string& what_arg);
350b57cec5SDimitry Andric    explicit xxx_error(const char*   what_arg);
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric    virtual const char* what() const noexcept // returns what_arg
380b57cec5SDimitry Andric};
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric}  // std
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric*/
430b57cec5SDimitry Andric
44*700637cbSDimitry Andric#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
45*700637cbSDimitry Andric#  include <__cxx03/stdexcept>
46*700637cbSDimitry Andric#else
470b57cec5SDimitry Andric#  include <__config>
4806c3fb27SDimitry Andric#  include <__exception/exception.h>
495f757f3fSDimitry Andric#  include <__fwd/string.h>
5006c3fb27SDimitry Andric#  include <__verbose_abort>
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
530b57cec5SDimitry Andric#    pragma GCC system_header
540b57cec5SDimitry Andric#  endif
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
59cb14a3feSDimitry Andricclass _LIBCPP_HIDDEN __libcpp_refstring {
600b57cec5SDimitry Andric  const char* __imp_;
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric  bool __uses_refcount() const;
63cb14a3feSDimitry Andric
640b57cec5SDimitry Andricpublic:
650b57cec5SDimitry Andric  explicit __libcpp_refstring(const char* __msg);
660b57cec5SDimitry Andric  __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
670b57cec5SDimitry Andric  __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
680b57cec5SDimitry Andric  ~__libcpp_refstring();
690b57cec5SDimitry Andric
7006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI const char* c_str() const _NOEXCEPT { return __imp_; }
710b57cec5SDimitry Andric};
720b57cec5SDimitry Andric#  endif // !_LIBCPP_ABI_VCRUNTIME
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
750b57cec5SDimitry Andric
760b57cec5SDimitry Andricnamespace std // purposefully not using versioning namespace
770b57cec5SDimitry Andric{
780b57cec5SDimitry Andric
79cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI logic_error : public exception {
800b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
81cb14a3feSDimitry Andric
820b57cec5SDimitry Andricprivate:
835f757f3fSDimitry Andric  std::__libcpp_refstring __imp_;
84cb14a3feSDimitry Andric
850b57cec5SDimitry Andricpublic:
860b57cec5SDimitry Andric  explicit logic_error(const string&);
870b57cec5SDimitry Andric  explicit logic_error(const char*);
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric  logic_error(const logic_error&) _NOEXCEPT;
900b57cec5SDimitry Andric  logic_error& operator=(const logic_error&) _NOEXCEPT;
910b57cec5SDimitry Andric
92bdd1243dSDimitry Andric  ~logic_error() _NOEXCEPT override;
930b57cec5SDimitry Andric
94bdd1243dSDimitry Andric  const char* what() const _NOEXCEPT override;
950b57cec5SDimitry Andric#  else
96cb14a3feSDimitry Andric
970b57cec5SDimitry Andricpublic:
985f757f3fSDimitry Andric  explicit logic_error(const std::string&); // Symbol uses versioned std::string
995f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit logic_error(const char* __s) : exception(__s) {}
1000b57cec5SDimitry Andric#  endif
1010b57cec5SDimitry Andric};
1020b57cec5SDimitry Andric
103cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI runtime_error : public exception {
1040b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
105cb14a3feSDimitry Andric
1060b57cec5SDimitry Andricprivate:
1075f757f3fSDimitry Andric  std::__libcpp_refstring __imp_;
108cb14a3feSDimitry Andric
1090b57cec5SDimitry Andricpublic:
1100b57cec5SDimitry Andric  explicit runtime_error(const string&);
1110b57cec5SDimitry Andric  explicit runtime_error(const char*);
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric  runtime_error(const runtime_error&) _NOEXCEPT;
1140b57cec5SDimitry Andric  runtime_error& operator=(const runtime_error&) _NOEXCEPT;
1150b57cec5SDimitry Andric
116bdd1243dSDimitry Andric  ~runtime_error() _NOEXCEPT override;
1170b57cec5SDimitry Andric
118bdd1243dSDimitry Andric  const char* what() const _NOEXCEPT override;
1190b57cec5SDimitry Andric#  else
120cb14a3feSDimitry Andric
1210b57cec5SDimitry Andricpublic:
1225f757f3fSDimitry Andric  explicit runtime_error(const std::string&); // Symbol uses versioned std::string
1235f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit runtime_error(const char* __s) : exception(__s) {}
1240b57cec5SDimitry Andric#  endif // _LIBCPP_ABI_VCRUNTIME
1250b57cec5SDimitry Andric};
1260b57cec5SDimitry Andric
127cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI domain_error : public logic_error {
1280b57cec5SDimitry Andricpublic:
1295f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit domain_error(const string& __s) : logic_error(__s) {}
1305f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit domain_error(const char* __s) : logic_error(__s) {}
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
13306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI domain_error(const domain_error&) _NOEXCEPT            = default;
1345f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI domain_error& operator=(const domain_error&) _NOEXCEPT = default;
135bdd1243dSDimitry Andric  ~domain_error() _NOEXCEPT override;
1360b57cec5SDimitry Andric#  endif
1370b57cec5SDimitry Andric};
1380b57cec5SDimitry Andric
139cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI invalid_argument : public logic_error {
1400b57cec5SDimitry Andricpublic:
1415f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const string& __s) : logic_error(__s) {}
1425f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const char* __s) : logic_error(__s) {}
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
14506c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT            = default;
1465f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI invalid_argument& operator=(const invalid_argument&) _NOEXCEPT = default;
147bdd1243dSDimitry Andric  ~invalid_argument() _NOEXCEPT override;
1480b57cec5SDimitry Andric#  endif
1490b57cec5SDimitry Andric};
1500b57cec5SDimitry Andric
151cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI length_error : public logic_error {
1520b57cec5SDimitry Andricpublic:
1535f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit length_error(const string& __s) : logic_error(__s) {}
1545f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit length_error(const char* __s) : logic_error(__s) {}
1550b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
15606c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT            = default;
1575f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI length_error& operator=(const length_error&) _NOEXCEPT = default;
158bdd1243dSDimitry Andric  ~length_error() _NOEXCEPT override;
1590b57cec5SDimitry Andric#  endif
1600b57cec5SDimitry Andric};
1610b57cec5SDimitry Andric
162cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI out_of_range : public logic_error {
1630b57cec5SDimitry Andricpublic:
1645f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const string& __s) : logic_error(__s) {}
1655f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const char* __s) : logic_error(__s) {}
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
16806c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT            = default;
1695f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI out_of_range& operator=(const out_of_range&) _NOEXCEPT = default;
170bdd1243dSDimitry Andric  ~out_of_range() _NOEXCEPT override;
1710b57cec5SDimitry Andric#  endif
1720b57cec5SDimitry Andric};
1730b57cec5SDimitry Andric
174cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI range_error : public runtime_error {
1750b57cec5SDimitry Andricpublic:
1765f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit range_error(const string& __s) : runtime_error(__s) {}
1775f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit range_error(const char* __s) : runtime_error(__s) {}
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
18006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT            = default;
1815f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI range_error& operator=(const range_error&) _NOEXCEPT = default;
182bdd1243dSDimitry Andric  ~range_error() _NOEXCEPT override;
1830b57cec5SDimitry Andric#  endif
1840b57cec5SDimitry Andric};
1850b57cec5SDimitry Andric
186cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI overflow_error : public runtime_error {
1870b57cec5SDimitry Andricpublic:
1885f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const string& __s) : runtime_error(__s) {}
1895f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const char* __s) : runtime_error(__s) {}
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
19206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT            = default;
1935f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI overflow_error& operator=(const overflow_error&) _NOEXCEPT = default;
194bdd1243dSDimitry Andric  ~overflow_error() _NOEXCEPT override;
1950b57cec5SDimitry Andric#  endif
1960b57cec5SDimitry Andric};
1970b57cec5SDimitry Andric
198cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI underflow_error : public runtime_error {
1990b57cec5SDimitry Andricpublic:
2005f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const string& __s) : runtime_error(__s) {}
2015f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const char* __s) : runtime_error(__s) {}
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VCRUNTIME
20406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT            = default;
2055f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI underflow_error& operator=(const underflow_error&) _NOEXCEPT = default;
206bdd1243dSDimitry Andric  ~underflow_error() _NOEXCEPT override;
2070b57cec5SDimitry Andric#  endif
2080b57cec5SDimitry Andric};
2090b57cec5SDimitry Andric
2100eae32dcSDimitry Andric} // namespace std
2110b57cec5SDimitry Andric
2120b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric// in the dylib
215*700637cbSDimitry Andric[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
2160b57cec5SDimitry Andric
217*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_logic_error(const char* __msg) {
218*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2190b57cec5SDimitry Andric  throw logic_error(__msg);
2200b57cec5SDimitry Andric#  else
22106c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2220b57cec5SDimitry Andric#  endif
2230b57cec5SDimitry Andric}
2240b57cec5SDimitry Andric
225*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_domain_error(const char* __msg) {
226*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2270b57cec5SDimitry Andric  throw domain_error(__msg);
2280b57cec5SDimitry Andric#  else
22906c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2300b57cec5SDimitry Andric#  endif
2310b57cec5SDimitry Andric}
2320b57cec5SDimitry Andric
233*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_invalid_argument(const char* __msg) {
234*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2350b57cec5SDimitry Andric  throw invalid_argument(__msg);
2360b57cec5SDimitry Andric#  else
23706c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2380b57cec5SDimitry Andric#  endif
2390b57cec5SDimitry Andric}
2400b57cec5SDimitry Andric
241*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_length_error(const char* __msg) {
242*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2430b57cec5SDimitry Andric  throw length_error(__msg);
2440b57cec5SDimitry Andric#  else
24506c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2460b57cec5SDimitry Andric#  endif
2470b57cec5SDimitry Andric}
2480b57cec5SDimitry Andric
249*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range(const char* __msg) {
250*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2510b57cec5SDimitry Andric  throw out_of_range(__msg);
2520b57cec5SDimitry Andric#  else
25306c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2540b57cec5SDimitry Andric#  endif
2550b57cec5SDimitry Andric}
2560b57cec5SDimitry Andric
257*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_range_error(const char* __msg) {
258*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2590b57cec5SDimitry Andric  throw range_error(__msg);
2600b57cec5SDimitry Andric#  else
26106c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2620b57cec5SDimitry Andric#  endif
2630b57cec5SDimitry Andric}
2640b57cec5SDimitry Andric
265*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_overflow_error(const char* __msg) {
266*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2670b57cec5SDimitry Andric  throw overflow_error(__msg);
2680b57cec5SDimitry Andric#  else
26906c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2700b57cec5SDimitry Andric#  endif
2710b57cec5SDimitry Andric}
2720b57cec5SDimitry Andric
273*700637cbSDimitry Andric[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_underflow_error(const char* __msg) {
274*700637cbSDimitry Andric#  if _LIBCPP_HAS_EXCEPTIONS
2750b57cec5SDimitry Andric  throw underflow_error(__msg);
2760b57cec5SDimitry Andric#  else
27706c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2780b57cec5SDimitry Andric#  endif
2790b57cec5SDimitry Andric}
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
2820b57cec5SDimitry Andric
28306c3fb27SDimitry Andric#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
284*700637cbSDimitry Andric#    include <cstddef>
28506c3fb27SDimitry Andric#    include <cstdlib>
28606c3fb27SDimitry Andric#    include <exception>
2875f757f3fSDimitry Andric#    include <iosfwd>
288*700637cbSDimitry Andric#    include <new>
28906c3fb27SDimitry Andric#  endif
290*700637cbSDimitry Andric#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
29106c3fb27SDimitry Andric
2920b57cec5SDimitry Andric#endif // _LIBCPP_STDEXCEPT
293