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_HAVE_STD_OPTIONAL
29
30#include <optional>
31
32namespace absl {
33using std::bad_optional_access;
34} // namespace absl
35
36#else // ABSL_HAVE_STD_OPTIONAL
37
38namespace absl {
39
40// -----------------------------------------------------------------------------
41// bad_optional_access
42// -----------------------------------------------------------------------------
43//
44// An `absl::bad_optional_access` type is an exception type that is thrown when
45// attempting to access an `absl::optional` object that does not contain a
46// value.
47//
48// Example:
49//
50// absl::optional<int> o;
51//
52// try {
53// int n = o.value();
54// } catch(const absl::bad_optional_access& e) {
55// std::cout << "Bad optional access: " << e.what() << '\n';
56// }
57class bad_optional_access : public std::exception {
58 public:
59 bad_optional_access() = default;
60 ~bad_optional_access() override;
61 const char* what() const noexcept override;
62};
63
64namespace optional_internal {
65
66// throw delegator
67[[noreturn]] void throw_bad_optional_access();
68
69} // namespace optional_internal
70} // namespace absl
71
72#endif // ABSL_HAVE_STD_OPTIONAL
73
74#endif // ABSL_TYPES_BAD_OPTIONAL_ACCESS_H_
75