xref: /freebsd/contrib/llvm-project/libcxx/include/stdexcept (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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
10#ifndef _LIBCPP_STDEXCEPT
11#define _LIBCPP_STDEXCEPT
12
13/*
14    stdexcept synopsis
15
16namespace std
17{
18
19class logic_error;
20class domain_error;
21class invalid_argument;
22class length_error;
23class out_of_range;
24class runtime_error;
25class range_error;
26class overflow_error;
27class underflow_error;
28
29for each class xxx_error:
30
31class xxx_error : public exception // at least indirectly
32{
33public:
34    explicit xxx_error(const string& what_arg);
35    explicit xxx_error(const char*   what_arg);
36
37    virtual const char* what() const noexcept // returns what_arg
38};
39
40}  // std
41
42*/
43
44#include <__config>
45#include <__exception/exception.h>
46#include <__fwd/string.h>
47#include <__verbose_abort>
48
49#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50#  pragma GCC system_header
51#endif
52
53_LIBCPP_BEGIN_NAMESPACE_STD
54
55#ifndef _LIBCPP_ABI_VCRUNTIME
56class _LIBCPP_HIDDEN __libcpp_refstring {
57  const char* __imp_;
58
59  bool __uses_refcount() const;
60
61public:
62  explicit __libcpp_refstring(const char* __msg);
63  __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
64  __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
65  ~__libcpp_refstring();
66
67  _LIBCPP_HIDE_FROM_ABI const char* c_str() const _NOEXCEPT { return __imp_; }
68};
69#endif // !_LIBCPP_ABI_VCRUNTIME
70
71_LIBCPP_END_NAMESPACE_STD
72
73namespace std // purposefully not using versioning namespace
74{
75
76class _LIBCPP_EXPORTED_FROM_ABI logic_error : public exception {
77#ifndef _LIBCPP_ABI_VCRUNTIME
78
79private:
80  std::__libcpp_refstring __imp_;
81
82public:
83  explicit logic_error(const string&);
84  explicit logic_error(const char*);
85
86  logic_error(const logic_error&) _NOEXCEPT;
87  logic_error& operator=(const logic_error&) _NOEXCEPT;
88
89  ~logic_error() _NOEXCEPT override;
90
91  const char* what() const _NOEXCEPT override;
92#else
93
94public:
95  explicit logic_error(const std::string&); // Symbol uses versioned std::string
96  _LIBCPP_HIDE_FROM_ABI explicit logic_error(const char* __s) : exception(__s) {}
97#endif
98};
99
100class _LIBCPP_EXPORTED_FROM_ABI runtime_error : public exception {
101#ifndef _LIBCPP_ABI_VCRUNTIME
102
103private:
104  std::__libcpp_refstring __imp_;
105
106public:
107  explicit runtime_error(const string&);
108  explicit runtime_error(const char*);
109
110  runtime_error(const runtime_error&) _NOEXCEPT;
111  runtime_error& operator=(const runtime_error&) _NOEXCEPT;
112
113  ~runtime_error() _NOEXCEPT override;
114
115  const char* what() const _NOEXCEPT override;
116#else
117
118public:
119  explicit runtime_error(const std::string&); // Symbol uses versioned std::string
120  _LIBCPP_HIDE_FROM_ABI explicit runtime_error(const char* __s) : exception(__s) {}
121#endif // _LIBCPP_ABI_VCRUNTIME
122};
123
124class _LIBCPP_EXPORTED_FROM_ABI domain_error : public logic_error {
125public:
126  _LIBCPP_HIDE_FROM_ABI explicit domain_error(const string& __s) : logic_error(__s) {}
127  _LIBCPP_HIDE_FROM_ABI explicit domain_error(const char* __s) : logic_error(__s) {}
128
129#ifndef _LIBCPP_ABI_VCRUNTIME
130  _LIBCPP_HIDE_FROM_ABI domain_error(const domain_error&) _NOEXCEPT            = default;
131  _LIBCPP_HIDE_FROM_ABI domain_error& operator=(const domain_error&) _NOEXCEPT = default;
132  ~domain_error() _NOEXCEPT override;
133#endif
134};
135
136class _LIBCPP_EXPORTED_FROM_ABI invalid_argument : public logic_error {
137public:
138  _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const string& __s) : logic_error(__s) {}
139  _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const char* __s) : logic_error(__s) {}
140
141#ifndef _LIBCPP_ABI_VCRUNTIME
142  _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT            = default;
143  _LIBCPP_HIDE_FROM_ABI invalid_argument& operator=(const invalid_argument&) _NOEXCEPT = default;
144  ~invalid_argument() _NOEXCEPT override;
145#endif
146};
147
148class _LIBCPP_EXPORTED_FROM_ABI length_error : public logic_error {
149public:
150  _LIBCPP_HIDE_FROM_ABI explicit length_error(const string& __s) : logic_error(__s) {}
151  _LIBCPP_HIDE_FROM_ABI explicit length_error(const char* __s) : logic_error(__s) {}
152#ifndef _LIBCPP_ABI_VCRUNTIME
153  _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT            = default;
154  _LIBCPP_HIDE_FROM_ABI length_error& operator=(const length_error&) _NOEXCEPT = default;
155  ~length_error() _NOEXCEPT override;
156#endif
157};
158
159class _LIBCPP_EXPORTED_FROM_ABI out_of_range : public logic_error {
160public:
161  _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const string& __s) : logic_error(__s) {}
162  _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const char* __s) : logic_error(__s) {}
163
164#ifndef _LIBCPP_ABI_VCRUNTIME
165  _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT            = default;
166  _LIBCPP_HIDE_FROM_ABI out_of_range& operator=(const out_of_range&) _NOEXCEPT = default;
167  ~out_of_range() _NOEXCEPT override;
168#endif
169};
170
171class _LIBCPP_EXPORTED_FROM_ABI range_error : public runtime_error {
172public:
173  _LIBCPP_HIDE_FROM_ABI explicit range_error(const string& __s) : runtime_error(__s) {}
174  _LIBCPP_HIDE_FROM_ABI explicit range_error(const char* __s) : runtime_error(__s) {}
175
176#ifndef _LIBCPP_ABI_VCRUNTIME
177  _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT            = default;
178  _LIBCPP_HIDE_FROM_ABI range_error& operator=(const range_error&) _NOEXCEPT = default;
179  ~range_error() _NOEXCEPT override;
180#endif
181};
182
183class _LIBCPP_EXPORTED_FROM_ABI overflow_error : public runtime_error {
184public:
185  _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const string& __s) : runtime_error(__s) {}
186  _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const char* __s) : runtime_error(__s) {}
187
188#ifndef _LIBCPP_ABI_VCRUNTIME
189  _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT            = default;
190  _LIBCPP_HIDE_FROM_ABI overflow_error& operator=(const overflow_error&) _NOEXCEPT = default;
191  ~overflow_error() _NOEXCEPT override;
192#endif
193};
194
195class _LIBCPP_EXPORTED_FROM_ABI underflow_error : public runtime_error {
196public:
197  _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const string& __s) : runtime_error(__s) {}
198  _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const char* __s) : runtime_error(__s) {}
199
200#ifndef _LIBCPP_ABI_VCRUNTIME
201  _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT            = default;
202  _LIBCPP_HIDE_FROM_ABI underflow_error& operator=(const underflow_error&) _NOEXCEPT = default;
203  ~underflow_error() _NOEXCEPT override;
204#endif
205};
206
207} // namespace std
208
209_LIBCPP_BEGIN_NAMESPACE_STD
210
211// in the dylib
212_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
213
214_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_logic_error(const char* __msg) {
215#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
216  throw logic_error(__msg);
217#else
218  _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
219#endif
220}
221
222_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_domain_error(const char* __msg) {
223#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
224  throw domain_error(__msg);
225#else
226  _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
227#endif
228}
229
230_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_invalid_argument(const char* __msg) {
231#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
232  throw invalid_argument(__msg);
233#else
234  _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
235#endif
236}
237
238_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_length_error(const char* __msg) {
239#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
240  throw length_error(__msg);
241#else
242  _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
243#endif
244}
245
246_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range(const char* __msg) {
247#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
248  throw out_of_range(__msg);
249#else
250  _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
251#endif
252}
253
254_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_range_error(const char* __msg) {
255#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
256  throw range_error(__msg);
257#else
258  _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
259#endif
260}
261
262_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_overflow_error(const char* __msg) {
263#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
264  throw overflow_error(__msg);
265#else
266  _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
267#endif
268}
269
270_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_underflow_error(const char* __msg) {
271#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
272  throw underflow_error(__msg);
273#else
274  _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
275#endif
276}
277
278_LIBCPP_END_NAMESPACE_STD
279
280#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
281#  include <cstdlib>
282#  include <exception>
283#  include <iosfwd>
284#endif
285
286#endif // _LIBCPP_STDEXCEPT
287