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 #include "absl/base/config.h" 22 #include "absl/base/internal/raw_logging.h" 23 24 namespace absl { 25 ABSL_NAMESPACE_BEGIN 26 namespace base_internal { 27 28 namespace { 29 template <typename T> Throw(const T & error)30[[noreturn]] void Throw(const T& error) { 31 #ifdef ABSL_HAVE_EXCEPTIONS 32 throw error; 33 #else 34 ABSL_RAW_LOG(FATAL, "%s", error.what()); 35 std::abort(); 36 #endif 37 } 38 } // namespace 39 ThrowStdLogicError(const std::string & what_arg)40void ThrowStdLogicError(const std::string& what_arg) { 41 Throw(std::logic_error(what_arg)); 42 } ThrowStdLogicError(const char * what_arg)43void ThrowStdLogicError(const char* what_arg) { 44 Throw(std::logic_error(what_arg)); 45 } ThrowStdInvalidArgument(const std::string & what_arg)46void ThrowStdInvalidArgument(const std::string& what_arg) { 47 Throw(std::invalid_argument(what_arg)); 48 } ThrowStdInvalidArgument(const char * what_arg)49void ThrowStdInvalidArgument(const char* what_arg) { 50 Throw(std::invalid_argument(what_arg)); 51 } 52 ThrowStdDomainError(const std::string & what_arg)53void ThrowStdDomainError(const std::string& what_arg) { 54 Throw(std::domain_error(what_arg)); 55 } ThrowStdDomainError(const char * what_arg)56void ThrowStdDomainError(const char* what_arg) { 57 Throw(std::domain_error(what_arg)); 58 } 59 ThrowStdLengthError(const std::string & what_arg)60void ThrowStdLengthError(const std::string& what_arg) { 61 Throw(std::length_error(what_arg)); 62 } ThrowStdLengthError(const char * what_arg)63void ThrowStdLengthError(const char* what_arg) { 64 Throw(std::length_error(what_arg)); 65 } 66 ThrowStdOutOfRange(const std::string & what_arg)67void ThrowStdOutOfRange(const std::string& what_arg) { 68 Throw(std::out_of_range(what_arg)); 69 } ThrowStdOutOfRange(const char * what_arg)70void ThrowStdOutOfRange(const char* what_arg) { 71 Throw(std::out_of_range(what_arg)); 72 } 73 ThrowStdRuntimeError(const std::string & what_arg)74void ThrowStdRuntimeError(const std::string& what_arg) { 75 Throw(std::runtime_error(what_arg)); 76 } ThrowStdRuntimeError(const char * what_arg)77void ThrowStdRuntimeError(const char* what_arg) { 78 Throw(std::runtime_error(what_arg)); 79 } 80 ThrowStdRangeError(const std::string & what_arg)81void ThrowStdRangeError(const std::string& what_arg) { 82 Throw(std::range_error(what_arg)); 83 } ThrowStdRangeError(const char * what_arg)84void ThrowStdRangeError(const char* what_arg) { 85 Throw(std::range_error(what_arg)); 86 } 87 ThrowStdOverflowError(const std::string & what_arg)88void ThrowStdOverflowError(const std::string& what_arg) { 89 Throw(std::overflow_error(what_arg)); 90 } ThrowStdOverflowError(const char * what_arg)91void ThrowStdOverflowError(const char* what_arg) { 92 Throw(std::overflow_error(what_arg)); 93 } 94 ThrowStdUnderflowError(const std::string & what_arg)95void ThrowStdUnderflowError(const std::string& what_arg) { 96 Throw(std::underflow_error(what_arg)); 97 } ThrowStdUnderflowError(const char * what_arg)98void ThrowStdUnderflowError(const char* what_arg) { 99 Throw(std::underflow_error(what_arg)); 100 } 101 ThrowStdBadFunctionCall()102void ThrowStdBadFunctionCall() { Throw(std::bad_function_call()); } 103 ThrowStdBadAlloc()104void ThrowStdBadAlloc() { Throw(std::bad_alloc()); } 105 106 } // namespace base_internal 107 ABSL_NAMESPACE_END 108 } // namespace absl 109