1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker
15*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/throw_delegate.h"
16*9356374aSAndroid Build Coastguard Worker
17*9356374aSAndroid Build Coastguard Worker #include <cstdlib>
18*9356374aSAndroid Build Coastguard Worker #include <functional>
19*9356374aSAndroid Build Coastguard Worker #include <new>
20*9356374aSAndroid Build Coastguard Worker #include <stdexcept>
21*9356374aSAndroid Build Coastguard Worker
22*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
23*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/raw_logging.h"
24*9356374aSAndroid Build Coastguard Worker
25*9356374aSAndroid Build Coastguard Worker namespace absl {
26*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
27*9356374aSAndroid Build Coastguard Worker namespace base_internal {
28*9356374aSAndroid Build Coastguard Worker
29*9356374aSAndroid Build Coastguard Worker // NOTE: The exception types, like `std::logic_error`, do not exist on all
30*9356374aSAndroid Build Coastguard Worker // platforms. (For example, the Android NDK does not have them.)
31*9356374aSAndroid Build Coastguard Worker // Therefore, their use must be guarded by `#ifdef` or equivalent.
32*9356374aSAndroid Build Coastguard Worker
ThrowStdLogicError(const std::string & what_arg)33*9356374aSAndroid Build Coastguard Worker void ThrowStdLogicError(const std::string& what_arg) {
34*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
35*9356374aSAndroid Build Coastguard Worker throw std::logic_error(what_arg);
36*9356374aSAndroid Build Coastguard Worker #else
37*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
38*9356374aSAndroid Build Coastguard Worker std::abort();
39*9356374aSAndroid Build Coastguard Worker #endif
40*9356374aSAndroid Build Coastguard Worker }
ThrowStdLogicError(const char * what_arg)41*9356374aSAndroid Build Coastguard Worker void ThrowStdLogicError(const char* what_arg) {
42*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
43*9356374aSAndroid Build Coastguard Worker throw std::logic_error(what_arg);
44*9356374aSAndroid Build Coastguard Worker #else
45*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
46*9356374aSAndroid Build Coastguard Worker std::abort();
47*9356374aSAndroid Build Coastguard Worker #endif
48*9356374aSAndroid Build Coastguard Worker }
ThrowStdInvalidArgument(const std::string & what_arg)49*9356374aSAndroid Build Coastguard Worker void ThrowStdInvalidArgument(const std::string& what_arg) {
50*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
51*9356374aSAndroid Build Coastguard Worker throw std::invalid_argument(what_arg);
52*9356374aSAndroid Build Coastguard Worker #else
53*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
54*9356374aSAndroid Build Coastguard Worker std::abort();
55*9356374aSAndroid Build Coastguard Worker #endif
56*9356374aSAndroid Build Coastguard Worker }
ThrowStdInvalidArgument(const char * what_arg)57*9356374aSAndroid Build Coastguard Worker void ThrowStdInvalidArgument(const char* what_arg) {
58*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
59*9356374aSAndroid Build Coastguard Worker throw std::invalid_argument(what_arg);
60*9356374aSAndroid Build Coastguard Worker #else
61*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
62*9356374aSAndroid Build Coastguard Worker std::abort();
63*9356374aSAndroid Build Coastguard Worker #endif
64*9356374aSAndroid Build Coastguard Worker }
65*9356374aSAndroid Build Coastguard Worker
ThrowStdDomainError(const std::string & what_arg)66*9356374aSAndroid Build Coastguard Worker void ThrowStdDomainError(const std::string& what_arg) {
67*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
68*9356374aSAndroid Build Coastguard Worker throw std::domain_error(what_arg);
69*9356374aSAndroid Build Coastguard Worker #else
70*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
71*9356374aSAndroid Build Coastguard Worker std::abort();
72*9356374aSAndroid Build Coastguard Worker #endif
73*9356374aSAndroid Build Coastguard Worker }
ThrowStdDomainError(const char * what_arg)74*9356374aSAndroid Build Coastguard Worker void ThrowStdDomainError(const char* what_arg) {
75*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
76*9356374aSAndroid Build Coastguard Worker throw std::domain_error(what_arg);
77*9356374aSAndroid Build Coastguard Worker #else
78*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
79*9356374aSAndroid Build Coastguard Worker std::abort();
80*9356374aSAndroid Build Coastguard Worker #endif
81*9356374aSAndroid Build Coastguard Worker }
82*9356374aSAndroid Build Coastguard Worker
ThrowStdLengthError(const std::string & what_arg)83*9356374aSAndroid Build Coastguard Worker void ThrowStdLengthError(const std::string& what_arg) {
84*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
85*9356374aSAndroid Build Coastguard Worker throw std::length_error(what_arg);
86*9356374aSAndroid Build Coastguard Worker #else
87*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
88*9356374aSAndroid Build Coastguard Worker std::abort();
89*9356374aSAndroid Build Coastguard Worker #endif
90*9356374aSAndroid Build Coastguard Worker }
ThrowStdLengthError(const char * what_arg)91*9356374aSAndroid Build Coastguard Worker void ThrowStdLengthError(const char* what_arg) {
92*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
93*9356374aSAndroid Build Coastguard Worker throw std::length_error(what_arg);
94*9356374aSAndroid Build Coastguard Worker #else
95*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
96*9356374aSAndroid Build Coastguard Worker std::abort();
97*9356374aSAndroid Build Coastguard Worker #endif
98*9356374aSAndroid Build Coastguard Worker }
99*9356374aSAndroid Build Coastguard Worker
ThrowStdOutOfRange(const std::string & what_arg)100*9356374aSAndroid Build Coastguard Worker void ThrowStdOutOfRange(const std::string& what_arg) {
101*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
102*9356374aSAndroid Build Coastguard Worker throw std::out_of_range(what_arg);
103*9356374aSAndroid Build Coastguard Worker #else
104*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
105*9356374aSAndroid Build Coastguard Worker std::abort();
106*9356374aSAndroid Build Coastguard Worker #endif
107*9356374aSAndroid Build Coastguard Worker }
ThrowStdOutOfRange(const char * what_arg)108*9356374aSAndroid Build Coastguard Worker void ThrowStdOutOfRange(const char* what_arg) {
109*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
110*9356374aSAndroid Build Coastguard Worker throw std::out_of_range(what_arg);
111*9356374aSAndroid Build Coastguard Worker #else
112*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
113*9356374aSAndroid Build Coastguard Worker std::abort();
114*9356374aSAndroid Build Coastguard Worker #endif
115*9356374aSAndroid Build Coastguard Worker }
116*9356374aSAndroid Build Coastguard Worker
ThrowStdRuntimeError(const std::string & what_arg)117*9356374aSAndroid Build Coastguard Worker void ThrowStdRuntimeError(const std::string& what_arg) {
118*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
119*9356374aSAndroid Build Coastguard Worker throw std::runtime_error(what_arg);
120*9356374aSAndroid Build Coastguard Worker #else
121*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
122*9356374aSAndroid Build Coastguard Worker std::abort();
123*9356374aSAndroid Build Coastguard Worker #endif
124*9356374aSAndroid Build Coastguard Worker }
ThrowStdRuntimeError(const char * what_arg)125*9356374aSAndroid Build Coastguard Worker void ThrowStdRuntimeError(const char* what_arg) {
126*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
127*9356374aSAndroid Build Coastguard Worker throw std::runtime_error(what_arg);
128*9356374aSAndroid Build Coastguard Worker #else
129*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
130*9356374aSAndroid Build Coastguard Worker std::abort();
131*9356374aSAndroid Build Coastguard Worker #endif
132*9356374aSAndroid Build Coastguard Worker }
133*9356374aSAndroid Build Coastguard Worker
ThrowStdRangeError(const std::string & what_arg)134*9356374aSAndroid Build Coastguard Worker void ThrowStdRangeError(const std::string& what_arg) {
135*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
136*9356374aSAndroid Build Coastguard Worker throw std::range_error(what_arg);
137*9356374aSAndroid Build Coastguard Worker #else
138*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
139*9356374aSAndroid Build Coastguard Worker std::abort();
140*9356374aSAndroid Build Coastguard Worker #endif
141*9356374aSAndroid Build Coastguard Worker }
ThrowStdRangeError(const char * what_arg)142*9356374aSAndroid Build Coastguard Worker void ThrowStdRangeError(const char* what_arg) {
143*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
144*9356374aSAndroid Build Coastguard Worker throw std::range_error(what_arg);
145*9356374aSAndroid Build Coastguard Worker #else
146*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
147*9356374aSAndroid Build Coastguard Worker std::abort();
148*9356374aSAndroid Build Coastguard Worker #endif
149*9356374aSAndroid Build Coastguard Worker }
150*9356374aSAndroid Build Coastguard Worker
ThrowStdOverflowError(const std::string & what_arg)151*9356374aSAndroid Build Coastguard Worker void ThrowStdOverflowError(const std::string& what_arg) {
152*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
153*9356374aSAndroid Build Coastguard Worker throw std::overflow_error(what_arg);
154*9356374aSAndroid Build Coastguard Worker #else
155*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
156*9356374aSAndroid Build Coastguard Worker std::abort();
157*9356374aSAndroid Build Coastguard Worker #endif
158*9356374aSAndroid Build Coastguard Worker }
ThrowStdOverflowError(const char * what_arg)159*9356374aSAndroid Build Coastguard Worker void ThrowStdOverflowError(const char* what_arg) {
160*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
161*9356374aSAndroid Build Coastguard Worker throw std::overflow_error(what_arg);
162*9356374aSAndroid Build Coastguard Worker #else
163*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
164*9356374aSAndroid Build Coastguard Worker std::abort();
165*9356374aSAndroid Build Coastguard Worker #endif
166*9356374aSAndroid Build Coastguard Worker }
167*9356374aSAndroid Build Coastguard Worker
ThrowStdUnderflowError(const std::string & what_arg)168*9356374aSAndroid Build Coastguard Worker void ThrowStdUnderflowError(const std::string& what_arg) {
169*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
170*9356374aSAndroid Build Coastguard Worker throw std::underflow_error(what_arg);
171*9356374aSAndroid Build Coastguard Worker #else
172*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
173*9356374aSAndroid Build Coastguard Worker std::abort();
174*9356374aSAndroid Build Coastguard Worker #endif
175*9356374aSAndroid Build Coastguard Worker }
ThrowStdUnderflowError(const char * what_arg)176*9356374aSAndroid Build Coastguard Worker void ThrowStdUnderflowError(const char* what_arg) {
177*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
178*9356374aSAndroid Build Coastguard Worker throw std::underflow_error(what_arg);
179*9356374aSAndroid Build Coastguard Worker #else
180*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "%s", what_arg);
181*9356374aSAndroid Build Coastguard Worker std::abort();
182*9356374aSAndroid Build Coastguard Worker #endif
183*9356374aSAndroid Build Coastguard Worker }
184*9356374aSAndroid Build Coastguard Worker
ThrowStdBadFunctionCall()185*9356374aSAndroid Build Coastguard Worker void ThrowStdBadFunctionCall() {
186*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
187*9356374aSAndroid Build Coastguard Worker throw std::bad_function_call();
188*9356374aSAndroid Build Coastguard Worker #else
189*9356374aSAndroid Build Coastguard Worker std::abort();
190*9356374aSAndroid Build Coastguard Worker #endif
191*9356374aSAndroid Build Coastguard Worker }
192*9356374aSAndroid Build Coastguard Worker
ThrowStdBadAlloc()193*9356374aSAndroid Build Coastguard Worker void ThrowStdBadAlloc() {
194*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
195*9356374aSAndroid Build Coastguard Worker throw std::bad_alloc();
196*9356374aSAndroid Build Coastguard Worker #else
197*9356374aSAndroid Build Coastguard Worker std::abort();
198*9356374aSAndroid Build Coastguard Worker #endif
199*9356374aSAndroid Build Coastguard Worker }
200*9356374aSAndroid Build Coastguard Worker
201*9356374aSAndroid Build Coastguard Worker } // namespace base_internal
202*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
203*9356374aSAndroid Build Coastguard Worker } // namespace absl
204