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