blob: 6eda619b8f448e308551522fa33657db95aab826 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- stdexcept --------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STDEXCEPT
11#define _LIBCPP_STDEXCEPT
12
13/*
14 stdexcept synopsis
15
16namespace std
17{
18
19class logic_error;
20 class domain_error;
21 class invalid_argument;
22 class length_error;
23 class out_of_range;
24class runtime_error;
25 class range_error;
26 class overflow_error;
27 class 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);
Howard Hinnant1f098bc2011-05-26 19:48:01 +000035 explicit xxx_error(const char* what_arg);
Howard Hinnantc51e1022010-05-11 19:42:16 +000036
Howard Hinnant1f098bc2011-05-26 19:48:01 +000037 virtual const char* what() const noexcept // returns what_arg
Howard Hinnantc51e1022010-05-11 19:42:16 +000038};
39
40} // std
41
42*/
43
44#include <__config>
45#include <exception>
46#include <iosfwd> // for string forward decl
Eric Fiselier279c3192016-09-06 21:25:27 +000047#ifdef _LIBCPP_NO_EXCEPTIONS
48#include <cstdlib>
49#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000050
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000051#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000052#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000053#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000054
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000055_LIBCPP_BEGIN_NAMESPACE_STD
Eric Fiselier1e655162016-10-25 19:33:14 +000056
57class _LIBCPP_HIDDEN __libcpp_refstring
58{
59 const char* __imp_;
60
61 bool __uses_refcount() const;
62public:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +000063 explicit __libcpp_refstring(const char* __msg);
64 __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
65 __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
Eric Fiselier1e655162016-10-25 19:33:14 +000066 ~__libcpp_refstring();
67
68 const char* c_str() const _NOEXCEPT {return __imp_;}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000069};
Eric Fiselier1e655162016-10-25 19:33:14 +000070
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000071_LIBCPP_END_NAMESPACE_STD
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000072
Howard Hinnantc51e1022010-05-11 19:42:16 +000073namespace std // purposefully not using versioning namespace
74{
75
76class _LIBCPP_EXCEPTION_ABI logic_error
77 : public exception
78{
79private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000080 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +000081public:
82 explicit logic_error(const string&);
83 explicit logic_error(const char*);
84
Howard Hinnant1f098bc2011-05-26 19:48:01 +000085 logic_error(const logic_error&) _NOEXCEPT;
86 logic_error& operator=(const logic_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000087
Howard Hinnant1f098bc2011-05-26 19:48:01 +000088 virtual ~logic_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000089
Howard Hinnant1f098bc2011-05-26 19:48:01 +000090 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000091};
92
93class _LIBCPP_EXCEPTION_ABI runtime_error
94 : public exception
95{
96private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000097 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +000098public:
99 explicit runtime_error(const string&);
100 explicit runtime_error(const char*);
101
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000102 runtime_error(const runtime_error&) _NOEXCEPT;
103 runtime_error& operator=(const runtime_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000105 virtual ~runtime_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000107 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108};
109
110class _LIBCPP_EXCEPTION_ABI domain_error
111 : public logic_error
112{
113public:
114 _LIBCPP_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {}
115 _LIBCPP_INLINE_VISIBILITY explicit domain_error(const char* __s) : logic_error(__s) {}
116
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000117 virtual ~domain_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118};
119
120class _LIBCPP_EXCEPTION_ABI invalid_argument
121 : public logic_error
122{
123public:
124 _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
125 _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s) : logic_error(__s) {}
126
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000127 virtual ~invalid_argument() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128};
129
130class _LIBCPP_EXCEPTION_ABI length_error
131 : public logic_error
132{
133public:
134 _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
135 _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s) : logic_error(__s) {}
136
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000137 virtual ~length_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000138};
139
140class _LIBCPP_EXCEPTION_ABI out_of_range
141 : public logic_error
142{
143public:
144 _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
145 _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s) : logic_error(__s) {}
146
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000147 virtual ~out_of_range() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148};
149
150class _LIBCPP_EXCEPTION_ABI range_error
151 : public runtime_error
152{
153public:
154 _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
155 _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s) : runtime_error(__s) {}
156
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000157 virtual ~range_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158};
159
160class _LIBCPP_EXCEPTION_ABI overflow_error
161 : public runtime_error
162{
163public:
164 _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
165 _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s) : runtime_error(__s) {}
166
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000167 virtual ~overflow_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168};
169
170class _LIBCPP_EXCEPTION_ABI underflow_error
171 : public runtime_error
172{
173public:
174 _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
175 _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s) : runtime_error(__s) {}
176
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000177 virtual ~underflow_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178};
179
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180} // std
181
Marshall Clow8fea1612016-08-25 15:09:01 +0000182_LIBCPP_BEGIN_NAMESPACE_STD
183
184// in the dylib
185_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
186
Louis Dionne16fe2952018-07-11 23:14:33 +0000187_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000188void __throw_logic_error(const char*__msg)
189{
190#ifndef _LIBCPP_NO_EXCEPTIONS
191 throw logic_error(__msg);
192#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000193 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000194 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000195#endif
196}
197
Louis Dionne16fe2952018-07-11 23:14:33 +0000198_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000199void __throw_domain_error(const char*__msg)
200{
201#ifndef _LIBCPP_NO_EXCEPTIONS
202 throw domain_error(__msg);
203#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000204 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000205 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000206#endif
207}
208
Louis Dionne16fe2952018-07-11 23:14:33 +0000209_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000210void __throw_invalid_argument(const char*__msg)
211{
212#ifndef _LIBCPP_NO_EXCEPTIONS
213 throw invalid_argument(__msg);
214#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000215 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000216 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000217#endif
218}
219
Louis Dionne16fe2952018-07-11 23:14:33 +0000220_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000221void __throw_length_error(const char*__msg)
222{
223#ifndef _LIBCPP_NO_EXCEPTIONS
224 throw length_error(__msg);
225#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000226 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000227 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000228#endif
229}
230
Louis Dionne16fe2952018-07-11 23:14:33 +0000231_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000232void __throw_out_of_range(const char*__msg)
233{
234#ifndef _LIBCPP_NO_EXCEPTIONS
235 throw out_of_range(__msg);
236#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000237 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000238 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000239#endif
240}
241
Louis Dionne16fe2952018-07-11 23:14:33 +0000242_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000243void __throw_range_error(const char*__msg)
244{
245#ifndef _LIBCPP_NO_EXCEPTIONS
246 throw range_error(__msg);
247#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000248 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000249 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000250#endif
251}
252
Louis Dionne16fe2952018-07-11 23:14:33 +0000253_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000254void __throw_overflow_error(const char*__msg)
255{
256#ifndef _LIBCPP_NO_EXCEPTIONS
257 throw overflow_error(__msg);
258#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000259 ((void)__msg);
260 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000261#endif
262}
263
Louis Dionne16fe2952018-07-11 23:14:33 +0000264_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000265void __throw_underflow_error(const char*__msg)
266{
267#ifndef _LIBCPP_NO_EXCEPTIONS
268 throw underflow_error(__msg);
269#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000270 ((void)__msg);
271 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000272#endif
273}
274
275_LIBCPP_END_NAMESPACE_STD
276
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277#endif // _LIBCPP_STDEXCEPT