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