xref: /freebsd/contrib/llvm-project/libcxx/include/stdexcept (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 Andric    class domain_error;
210b57cec5SDimitry Andric    class invalid_argument;
220b57cec5SDimitry Andric    class length_error;
230b57cec5SDimitry Andric    class out_of_range;
240b57cec5SDimitry Andricclass runtime_error;
250b57cec5SDimitry Andric    class range_error;
260b57cec5SDimitry Andric    class overflow_error;
270b57cec5SDimitry Andric    class 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
4481ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
450b57cec5SDimitry Andric#include <__config>
46*06c3fb27SDimitry Andric#include <__exception/exception.h>
47*06c3fb27SDimitry Andric#include <__verbose_abort>
480b57cec5SDimitry Andric#include <iosfwd>  // for string forward decl
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
510b57cec5SDimitry Andric#  pragma GCC system_header
520b57cec5SDimitry Andric#endif
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
570b57cec5SDimitry Andricclass _LIBCPP_HIDDEN __libcpp_refstring
580b57cec5SDimitry Andric{
590b57cec5SDimitry Andric    const char* __imp_;
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric    bool __uses_refcount() const;
620b57cec5SDimitry Andricpublic:
630b57cec5SDimitry Andric    explicit __libcpp_refstring(const char* __msg);
640b57cec5SDimitry Andric    __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
650b57cec5SDimitry Andric    __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
660b57cec5SDimitry Andric    ~__libcpp_refstring();
670b57cec5SDimitry Andric
68*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI const char* c_str() const _NOEXCEPT {return __imp_;}
690b57cec5SDimitry Andric};
700b57cec5SDimitry Andric#endif // !_LIBCPP_ABI_VCRUNTIME
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
730b57cec5SDimitry Andric
740b57cec5SDimitry Andricnamespace std  // purposefully not using versioning namespace
750b57cec5SDimitry Andric{
760b57cec5SDimitry Andric
77*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI logic_error
780b57cec5SDimitry Andric    : public exception
790b57cec5SDimitry Andric{
800b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
810b57cec5SDimitry Andricprivate:
820b57cec5SDimitry Andric    _VSTD::__libcpp_refstring __imp_;
830b57cec5SDimitry Andricpublic:
840b57cec5SDimitry Andric    explicit logic_error(const string&);
850b57cec5SDimitry Andric    explicit logic_error(const char*);
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric    logic_error(const logic_error&) _NOEXCEPT;
880b57cec5SDimitry Andric    logic_error& operator=(const logic_error&) _NOEXCEPT;
890b57cec5SDimitry Andric
90bdd1243dSDimitry Andric    ~logic_error() _NOEXCEPT override;
910b57cec5SDimitry Andric
92bdd1243dSDimitry Andric    const char* what() const _NOEXCEPT override;
930b57cec5SDimitry Andric#else
940b57cec5SDimitry Andricpublic:
950b57cec5SDimitry Andric    explicit logic_error(const _VSTD::string&); // Symbol uses versioned std::string
960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit logic_error(const char* __s) : exception(__s) {}
970b57cec5SDimitry Andric#endif
980b57cec5SDimitry Andric};
990b57cec5SDimitry Andric
100*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI runtime_error
1010b57cec5SDimitry Andric    : public exception
1020b57cec5SDimitry Andric{
1030b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
1040b57cec5SDimitry Andricprivate:
1050b57cec5SDimitry Andric    _VSTD::__libcpp_refstring __imp_;
1060b57cec5SDimitry Andricpublic:
1070b57cec5SDimitry Andric    explicit runtime_error(const string&);
1080b57cec5SDimitry Andric    explicit runtime_error(const char*);
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric    runtime_error(const runtime_error&) _NOEXCEPT;
1110b57cec5SDimitry Andric    runtime_error& operator=(const runtime_error&) _NOEXCEPT;
1120b57cec5SDimitry Andric
113bdd1243dSDimitry Andric    ~runtime_error() _NOEXCEPT override;
1140b57cec5SDimitry Andric
115bdd1243dSDimitry Andric    const char* what() const _NOEXCEPT override;
1160b57cec5SDimitry Andric#else
1170b57cec5SDimitry Andricpublic:
1180b57cec5SDimitry Andric   explicit runtime_error(const _VSTD::string&); // Symbol uses versioned std::string
1190b57cec5SDimitry Andric   _LIBCPP_INLINE_VISIBILITY explicit runtime_error(const char* __s) : exception(__s) {}
1200b57cec5SDimitry Andric#endif // _LIBCPP_ABI_VCRUNTIME
1210b57cec5SDimitry Andric};
1220b57cec5SDimitry Andric
123*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI domain_error
1240b57cec5SDimitry Andric    : public logic_error
1250b57cec5SDimitry Andric{
1260b57cec5SDimitry Andricpublic:
1270b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {}
1280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit domain_error(const char* __s)   : logic_error(__s) {}
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
131*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI domain_error(const domain_error&) _NOEXCEPT = default;
132bdd1243dSDimitry Andric    ~domain_error() _NOEXCEPT override;
1330b57cec5SDimitry Andric#endif
1340b57cec5SDimitry Andric};
1350b57cec5SDimitry Andric
136*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI invalid_argument
1370b57cec5SDimitry Andric    : public logic_error
1380b57cec5SDimitry Andric{
1390b57cec5SDimitry Andricpublic:
1400b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
1410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s)   : logic_error(__s) {}
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
144*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT = default;
145bdd1243dSDimitry Andric    ~invalid_argument() _NOEXCEPT override;
1460b57cec5SDimitry Andric#endif
1470b57cec5SDimitry Andric};
1480b57cec5SDimitry Andric
149*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI length_error
1500b57cec5SDimitry Andric    : public logic_error
1510b57cec5SDimitry Andric{
1520b57cec5SDimitry Andricpublic:
1530b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
1540b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s)   : logic_error(__s) {}
1550b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
156*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT = default;
157bdd1243dSDimitry Andric    ~length_error() _NOEXCEPT override;
1580b57cec5SDimitry Andric#endif
1590b57cec5SDimitry Andric};
1600b57cec5SDimitry Andric
161*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI out_of_range
1620b57cec5SDimitry Andric    : public logic_error
1630b57cec5SDimitry Andric{
1640b57cec5SDimitry Andricpublic:
1650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
1660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s)   : logic_error(__s) {}
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
169*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT = default;
170bdd1243dSDimitry Andric    ~out_of_range() _NOEXCEPT override;
1710b57cec5SDimitry Andric#endif
1720b57cec5SDimitry Andric};
1730b57cec5SDimitry Andric
174*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI range_error
1750b57cec5SDimitry Andric    : public runtime_error
1760b57cec5SDimitry Andric{
1770b57cec5SDimitry Andricpublic:
1780b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
1790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s)   : runtime_error(__s) {}
1800b57cec5SDimitry Andric
1810b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
182*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT = default;
183bdd1243dSDimitry Andric    ~range_error() _NOEXCEPT override;
1840b57cec5SDimitry Andric#endif
1850b57cec5SDimitry Andric};
1860b57cec5SDimitry Andric
187*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI overflow_error
1880b57cec5SDimitry Andric    : public runtime_error
1890b57cec5SDimitry Andric{
1900b57cec5SDimitry Andricpublic:
1910b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
1920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s)   : runtime_error(__s) {}
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
195*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT = default;
196bdd1243dSDimitry Andric    ~overflow_error() _NOEXCEPT override;
1970b57cec5SDimitry Andric#endif
1980b57cec5SDimitry Andric};
1990b57cec5SDimitry Andric
200*06c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI underflow_error
2010b57cec5SDimitry Andric    : public runtime_error
2020b57cec5SDimitry Andric{
2030b57cec5SDimitry Andricpublic:
2040b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
2050b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s)   : runtime_error(__s) {}
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VCRUNTIME
208*06c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT = default;
209bdd1243dSDimitry Andric    ~underflow_error() _NOEXCEPT override;
2100b57cec5SDimitry Andric#endif
2110b57cec5SDimitry Andric};
2120b57cec5SDimitry Andric
2130eae32dcSDimitry Andric} // namespace std
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric// in the dylib
218*06c3fb27SDimitry Andric_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2210b57cec5SDimitry Andricvoid __throw_logic_error(const char*__msg)
2220b57cec5SDimitry Andric{
223*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2240b57cec5SDimitry Andric    throw logic_error(__msg);
2250b57cec5SDimitry Andric#else
226*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2270b57cec5SDimitry Andric#endif
2280b57cec5SDimitry Andric}
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2310b57cec5SDimitry Andricvoid __throw_domain_error(const char*__msg)
2320b57cec5SDimitry Andric{
233*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2340b57cec5SDimitry Andric    throw domain_error(__msg);
2350b57cec5SDimitry Andric#else
236*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2370b57cec5SDimitry Andric#endif
2380b57cec5SDimitry Andric}
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2410b57cec5SDimitry Andricvoid __throw_invalid_argument(const char*__msg)
2420b57cec5SDimitry Andric{
243*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2440b57cec5SDimitry Andric    throw invalid_argument(__msg);
2450b57cec5SDimitry Andric#else
246*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2470b57cec5SDimitry Andric#endif
2480b57cec5SDimitry Andric}
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2510b57cec5SDimitry Andricvoid __throw_length_error(const char*__msg)
2520b57cec5SDimitry Andric{
253*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2540b57cec5SDimitry Andric    throw length_error(__msg);
2550b57cec5SDimitry Andric#else
256*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2570b57cec5SDimitry Andric#endif
2580b57cec5SDimitry Andric}
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2610b57cec5SDimitry Andricvoid __throw_out_of_range(const char*__msg)
2620b57cec5SDimitry Andric{
263*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2640b57cec5SDimitry Andric    throw out_of_range(__msg);
2650b57cec5SDimitry Andric#else
266*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2670b57cec5SDimitry Andric#endif
2680b57cec5SDimitry Andric}
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2710b57cec5SDimitry Andricvoid __throw_range_error(const char*__msg)
2720b57cec5SDimitry Andric{
273*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2740b57cec5SDimitry Andric    throw range_error(__msg);
2750b57cec5SDimitry Andric#else
276*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2770b57cec5SDimitry Andric#endif
2780b57cec5SDimitry Andric}
2790b57cec5SDimitry Andric
2800b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2810b57cec5SDimitry Andricvoid __throw_overflow_error(const char*__msg)
2820b57cec5SDimitry Andric{
283*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2840b57cec5SDimitry Andric    throw overflow_error(__msg);
2850b57cec5SDimitry Andric#else
286*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2870b57cec5SDimitry Andric#endif
2880b57cec5SDimitry Andric}
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2910b57cec5SDimitry Andricvoid __throw_underflow_error(const char*__msg)
2920b57cec5SDimitry Andric{
293*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2940b57cec5SDimitry Andric    throw underflow_error(__msg);
2950b57cec5SDimitry Andric#else
296*06c3fb27SDimitry Andric    _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
2970b57cec5SDimitry Andric#endif
2980b57cec5SDimitry Andric}
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
3010b57cec5SDimitry Andric
302*06c3fb27SDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
303*06c3fb27SDimitry Andric#  include <cstdlib>
304*06c3fb27SDimitry Andric#  include <exception>
305*06c3fb27SDimitry Andric#endif
306*06c3fb27SDimitry Andric
3070b57cec5SDimitry Andric#endif // _LIBCPP_STDEXCEPT
308