1 // Copyright 2018 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 // ----------------------------------------------------------------------------- 16 // bad_optional_access.h 17 // ----------------------------------------------------------------------------- 18 // 19 // This header file defines the `absl::bad_optional_access` type. 20 21 #ifndef ABSL_TYPES_BAD_OPTIONAL_ACCESS_H_ 22 #define ABSL_TYPES_BAD_OPTIONAL_ACCESS_H_ 23 24 #include <stdexcept> 25 26 #include "absl/base/config.h" 27 28 #ifdef ABSL_USES_STD_OPTIONAL 29 30 #include <optional> 31 32 namespace absl { 33 ABSL_NAMESPACE_BEGIN 34 using std::bad_optional_access; 35 ABSL_NAMESPACE_END 36 } // namespace absl 37 38 #else // ABSL_USES_STD_OPTIONAL 39 40 namespace absl { 41 ABSL_NAMESPACE_BEGIN 42 43 // ----------------------------------------------------------------------------- 44 // bad_optional_access 45 // ----------------------------------------------------------------------------- 46 // 47 // An `absl::bad_optional_access` type is an exception type that is thrown when 48 // attempting to access an `absl::optional` object that does not contain a 49 // value. 50 // 51 // Example: 52 // 53 // absl::optional<int> o; 54 // 55 // try { 56 // int n = o.value(); 57 // } catch(const absl::bad_optional_access& e) { 58 // std::cout << "Bad optional access: " << e.what() << '\n'; 59 // } 60 class bad_optional_access : public std::exception { 61 public: 62 bad_optional_access() = default; 63 ~bad_optional_access() override; 64 const char* what() const noexcept override; 65 }; 66 67 namespace optional_internal { 68 69 // throw delegator 70 [[noreturn]] void throw_bad_optional_access(); 71 72 } // namespace optional_internal 73 ABSL_NAMESPACE_END 74 } // namespace absl 75 76 #endif // ABSL_USES_STD_OPTIONAL 77 78 #endif // ABSL_TYPES_BAD_OPTIONAL_ACCESS_H_ 79