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 exception types, like `std::logic_error`, do not exist on all
30 // platforms. (For example, the Android NDK does not have them.)
31 // Therefore, their use must be guarded by `#ifdef` or equivalent.
32
ThrowStdLogicError(const std::string & what_arg)33 void ThrowStdLogicError(const std::string& what_arg) {
34 #ifdef ABSL_HAVE_EXCEPTIONS
35 throw std::logic_error(what_arg);
36 #else
37 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
38 std::abort();
39 #endif
40 }
ThrowStdLogicError(const char * what_arg)41 void ThrowStdLogicError(const char* what_arg) {
42 #ifdef ABSL_HAVE_EXCEPTIONS
43 throw std::logic_error(what_arg);
44 #else
45 ABSL_RAW_LOG(FATAL, "%s", what_arg);
46 std::abort();
47 #endif
48 }
ThrowStdInvalidArgument(const std::string & what_arg)49 void ThrowStdInvalidArgument(const std::string& what_arg) {
50 #ifdef ABSL_HAVE_EXCEPTIONS
51 throw std::invalid_argument(what_arg);
52 #else
53 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
54 std::abort();
55 #endif
56 }
ThrowStdInvalidArgument(const char * what_arg)57 void ThrowStdInvalidArgument(const char* what_arg) {
58 #ifdef ABSL_HAVE_EXCEPTIONS
59 throw std::invalid_argument(what_arg);
60 #else
61 ABSL_RAW_LOG(FATAL, "%s", what_arg);
62 std::abort();
63 #endif
64 }
65
ThrowStdDomainError(const std::string & what_arg)66 void ThrowStdDomainError(const std::string& what_arg) {
67 #ifdef ABSL_HAVE_EXCEPTIONS
68 throw std::domain_error(what_arg);
69 #else
70 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
71 std::abort();
72 #endif
73 }
ThrowStdDomainError(const char * what_arg)74 void ThrowStdDomainError(const char* what_arg) {
75 #ifdef ABSL_HAVE_EXCEPTIONS
76 throw std::domain_error(what_arg);
77 #else
78 ABSL_RAW_LOG(FATAL, "%s", what_arg);
79 std::abort();
80 #endif
81 }
82
ThrowStdLengthError(const std::string & what_arg)83 void ThrowStdLengthError(const std::string& what_arg) {
84 #ifdef ABSL_HAVE_EXCEPTIONS
85 throw std::length_error(what_arg);
86 #else
87 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
88 std::abort();
89 #endif
90 }
ThrowStdLengthError(const char * what_arg)91 void ThrowStdLengthError(const char* what_arg) {
92 #ifdef ABSL_HAVE_EXCEPTIONS
93 throw std::length_error(what_arg);
94 #else
95 ABSL_RAW_LOG(FATAL, "%s", what_arg);
96 std::abort();
97 #endif
98 }
99
ThrowStdOutOfRange(const std::string & what_arg)100 void ThrowStdOutOfRange(const std::string& what_arg) {
101 #ifdef ABSL_HAVE_EXCEPTIONS
102 throw std::out_of_range(what_arg);
103 #else
104 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
105 std::abort();
106 #endif
107 }
ThrowStdOutOfRange(const char * what_arg)108 void ThrowStdOutOfRange(const char* what_arg) {
109 #ifdef ABSL_HAVE_EXCEPTIONS
110 throw std::out_of_range(what_arg);
111 #else
112 ABSL_RAW_LOG(FATAL, "%s", what_arg);
113 std::abort();
114 #endif
115 }
116
ThrowStdRuntimeError(const std::string & what_arg)117 void ThrowStdRuntimeError(const std::string& what_arg) {
118 #ifdef ABSL_HAVE_EXCEPTIONS
119 throw std::runtime_error(what_arg);
120 #else
121 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
122 std::abort();
123 #endif
124 }
ThrowStdRuntimeError(const char * what_arg)125 void ThrowStdRuntimeError(const char* what_arg) {
126 #ifdef ABSL_HAVE_EXCEPTIONS
127 throw std::runtime_error(what_arg);
128 #else
129 ABSL_RAW_LOG(FATAL, "%s", what_arg);
130 std::abort();
131 #endif
132 }
133
ThrowStdRangeError(const std::string & what_arg)134 void ThrowStdRangeError(const std::string& what_arg) {
135 #ifdef ABSL_HAVE_EXCEPTIONS
136 throw std::range_error(what_arg);
137 #else
138 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
139 std::abort();
140 #endif
141 }
ThrowStdRangeError(const char * what_arg)142 void ThrowStdRangeError(const char* what_arg) {
143 #ifdef ABSL_HAVE_EXCEPTIONS
144 throw std::range_error(what_arg);
145 #else
146 ABSL_RAW_LOG(FATAL, "%s", what_arg);
147 std::abort();
148 #endif
149 }
150
ThrowStdOverflowError(const std::string & what_arg)151 void ThrowStdOverflowError(const std::string& what_arg) {
152 #ifdef ABSL_HAVE_EXCEPTIONS
153 throw std::overflow_error(what_arg);
154 #else
155 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
156 std::abort();
157 #endif
158 }
ThrowStdOverflowError(const char * what_arg)159 void ThrowStdOverflowError(const char* what_arg) {
160 #ifdef ABSL_HAVE_EXCEPTIONS
161 throw std::overflow_error(what_arg);
162 #else
163 ABSL_RAW_LOG(FATAL, "%s", what_arg);
164 std::abort();
165 #endif
166 }
167
ThrowStdUnderflowError(const std::string & what_arg)168 void ThrowStdUnderflowError(const std::string& what_arg) {
169 #ifdef ABSL_HAVE_EXCEPTIONS
170 throw std::underflow_error(what_arg);
171 #else
172 ABSL_RAW_LOG(FATAL, "%s", what_arg.c_str());
173 std::abort();
174 #endif
175 }
ThrowStdUnderflowError(const char * what_arg)176 void ThrowStdUnderflowError(const char* what_arg) {
177 #ifdef ABSL_HAVE_EXCEPTIONS
178 throw std::underflow_error(what_arg);
179 #else
180 ABSL_RAW_LOG(FATAL, "%s", what_arg);
181 std::abort();
182 #endif
183 }
184
ThrowStdBadFunctionCall()185 void ThrowStdBadFunctionCall() {
186 #ifdef ABSL_HAVE_EXCEPTIONS
187 throw std::bad_function_call();
188 #else
189 std::abort();
190 #endif
191 }
192
ThrowStdBadAlloc()193 void ThrowStdBadAlloc() {
194 #ifdef ABSL_HAVE_EXCEPTIONS
195 throw std::bad_alloc();
196 #else
197 std::abort();
198 #endif
199 }
200
201 } // namespace base_internal
202 ABSL_NAMESPACE_END
203 } // namespace absl
204