2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <folly/ExceptionWrapper.h>
20 #include <folly/Likely.h>
21 #include <folly/Memory.h>
22 #include <folly/Portability.h>
23 #include <folly/Unit.h>
26 #include <type_traits>
31 class TryException : public std::logic_error {
33 using std::logic_error::logic_error;
36 class UsingUninitializedTry : public TryException {
38 UsingUninitializedTry() : TryException("Using uninitialized try") {}
42 * Try<T> is a wrapper that contains either an instance of T, an exception, or
43 * nothing. Exceptions are stored as exception_wrappers so that the user can
44 * minimize rethrows if so desired.
46 * To represent success or a captured exception, use Try<Unit>.
50 static_assert(!std::is_reference<T>::value,
51 "Try may not be used with reference types");
61 * The value type for the Try
63 typedef T element_type;
66 * Construct an empty Try
68 Try() : contains_(Contains::NOTHING) {}
71 * Construct a Try with a value by copy
73 * @param v The value to copy in
75 explicit Try(const T& v) : contains_(Contains::VALUE), value_(v) {}
78 * Construct a Try with a value by move
80 * @param v The value to move in
82 explicit Try(T&& v) : contains_(Contains::VALUE), value_(std::move(v)) {}
84 /// Implicit conversion from Try<void> to Try<Unit>
85 template <class T2 = T>
87 Try(typename std::enable_if<std::is_same<Unit, T2>::value,
88 Try<void> const&>::type t);
91 * Construct a Try with an exception_wrapper
93 * @param e The exception_wrapper
95 explicit Try(exception_wrapper e)
96 : contains_(Contains::EXCEPTION), e_(std::move(e)) {}
100 * Construct a Try with an exception_pointer
102 * @param ep The exception_pointer. Will be rethrown.
104 FOLLY_DEPRECATED("use Try(exception_wrapper)")
105 explicit Try(std::exception_ptr ep)
106 : contains_(Contains::EXCEPTION) {
108 std::rethrow_exception(ep);
109 } catch (std::exception& e) {
110 e_ = exception_wrapper(std::current_exception(), e);
112 e_ = exception_wrapper(std::current_exception());
117 Try(Try<T>&& t) noexcept;
119 Try& operator=(Try<T>&& t) noexcept;
124 Try& operator=(const Try& t);
129 * Get a mutable reference to the contained value. If the Try contains an
130 * exception it will be rethrown.
132 * @returns mutable reference to the contained value
136 * Get a rvalue reference to the contained value. If the Try contains an
137 * exception it will be rethrown.
139 * @returns rvalue reference to the contained value
143 * Get a const reference to the contained value. If the Try contains an
144 * exception it will be rethrown.
146 * @returns const reference to the contained value
148 const T& value() const&;
151 * If the Try contains an exception, rethrow it. Otherwise do nothing.
153 void throwIfFailed() const;
156 * Const dereference operator. If the Try contains an exception it will be
159 * @returns const reference to the contained value
161 const T& operator*() const { return value(); }
163 * Dereference operator. If the Try contains an exception it will be rethrown.
165 * @returns mutable reference to the contained value
167 T& operator*() { return value(); }
170 * Const arrow operator. If the Try contains an exception it will be
173 * @returns const reference to the contained value
175 const T* operator->() const { return &value(); }
177 * Arrow operator. If the Try contains an exception it will be rethrown.
179 * @returns mutable reference to the contained value
181 T* operator->() { return &value(); }
184 * @returns True if the Try contains a value, false otherwise
186 bool hasValue() const { return contains_ == Contains::VALUE; }
188 * @returns True if the Try contains an exception, false otherwise
190 bool hasException() const { return contains_ == Contains::EXCEPTION; }
193 * @returns True if the Try contains an exception of type Ex, false otherwise
196 bool hasException() const {
197 return hasException() && e_.is_compatible_with<Ex>();
200 exception_wrapper& exception() {
201 if (UNLIKELY(!hasException())) {
202 throw TryException("exception(): Try does not contain an exception");
207 const exception_wrapper& exception() const {
208 if (UNLIKELY(!hasException())) {
209 throw TryException("exception(): Try does not contain an exception");
215 * @returns a pointer to the `std::exception` held by `*this`, if one is held;
216 * otherwise, returns `nullptr`.
218 std::exception* tryGetExceptionObject() {
219 return hasException() ? e_.get_exception() : nullptr;
221 std::exception const* tryGetExceptionObject() const {
222 return hasException() ? e_.get_exception() : nullptr;
226 * @returns a pointer to the `Ex` held by `*this`, if it holds an object whose
227 * type `From` permits `std::is_convertible<From*, Ex*>`; otherwise,
231 E* tryGetExceptionObject() {
232 return hasException() ? e_.get_exception<E>() : nullptr;
235 E const* tryGetExceptionObject() const {
236 return hasException() ? e_.get_exception<E>() : nullptr;
240 * If the Try contains an exception and it is of type Ex, execute func(Ex)
242 * @param func a function that takes a single parameter of type const Ex&
244 * @returns True if the Try held an Ex and func was executed, false otherwise
246 template <class Ex, class F>
247 bool withException(F func) {
248 if (!hasException()) {
251 return e_.with_exception(std::move(func));
253 template <class Ex, class F>
254 bool withException(F func) const {
255 if (!hasException()) {
258 return e_.with_exception(std::move(func));
261 template <bool isTry, typename R>
262 typename std::enable_if<isTry, R>::type get() {
263 return std::forward<R>(*this);
266 template <bool isTry, typename R>
267 typename std::enable_if<!isTry, R>::type get() {
268 return std::forward<R>(value());
275 exception_wrapper e_;
280 * Specialization of Try for void value type. Encapsulates either success or an
287 * The value type for the Try
289 typedef void element_type;
291 // Construct a Try holding a successful and void result
292 Try() : hasValue_(true) {}
295 * Construct a Try with an exception_wrapper
297 * @param e The exception_wrapper
299 explicit Try(exception_wrapper e) : hasValue_(false), e_(std::move(e)) {}
303 * Construct a Try with an exception_pointer
305 * @param ep The exception_pointer. Will be rethrown.
307 FOLLY_DEPRECATED("use Try(exception_wrapper)")
308 explicit Try(std::exception_ptr ep) : hasValue_(false) {
310 std::rethrow_exception(ep);
311 } catch (const std::exception& e) {
312 e_ = exception_wrapper(std::current_exception(), e);
314 e_ = exception_wrapper(std::current_exception());
319 Try& operator=(const Try<void>& t) {
320 hasValue_ = t.hasValue_;
325 Try(const Try<void>& t) {
329 // If the Try contains an exception, throws it
330 void value() const { throwIfFailed(); }
331 // Dereference operator. If the Try contains an exception, throws it
332 void operator*() const { return value(); }
334 // If the Try contains an exception, throws it
335 inline void throwIfFailed() const;
337 // @returns False if the Try contains an exception, true otherwise
338 bool hasValue() const { return hasValue_; }
339 // @returns True if the Try contains an exception, false otherwise
340 bool hasException() const { return !hasValue_; }
342 // @returns True if the Try contains an exception of type Ex, false otherwise
344 bool hasException() const {
345 return hasException() && e_.is_compatible_with<Ex>();
349 * @throws TryException if the Try doesn't contain an exception
351 * @returns mutable reference to the exception contained by this Try
353 exception_wrapper& exception() {
354 if (UNLIKELY(!hasException())) {
355 throw TryException("exception(): Try does not contain an exception");
360 const exception_wrapper& exception() const {
361 if (UNLIKELY(!hasException())) {
362 throw TryException("exception(): Try does not contain an exception");
368 * @returns a pointer to the `std::exception` held by `*this`, if one is held;
369 * otherwise, returns `nullptr`.
371 std::exception* tryGetExceptionObject() {
372 return hasException() ? e_.get_exception() : nullptr;
374 std::exception const* tryGetExceptionObject() const {
375 return hasException() ? e_.get_exception() : nullptr;
379 * @returns a pointer to the `Ex` held by `*this`, if it holds an object whose
380 * type `From` permits `std::is_convertible<From*, Ex*>`; otherwise,
384 E* tryGetExceptionObject() {
385 return hasException() ? e_.get_exception<E>() : nullptr;
388 E const* tryGetExceptionObject() const {
389 return hasException() ? e_.get_exception<E>() : nullptr;
393 * If the Try contains an exception and it is of type Ex, execute func(Ex)
395 * @param func a function that takes a single parameter of type const Ex&
397 * @returns True if the Try held an Ex and func was executed, false otherwise
399 template <class Ex, class F>
400 bool withException(F func) {
401 if (!hasException()) {
404 return e_.with_exception(std::move(func));
406 template <class Ex, class F>
407 bool withException(F func) const {
408 if (!hasException()) {
411 return e_.with_exception(std::move(func));
414 template <bool, typename R>
416 return std::forward<R>(*this);
421 exception_wrapper e_;
425 * Extracts value from try and returns it. Throws if try contained an exception.
427 * @param t Try to extract value from
429 * @returns value contained in t
431 template <typename T>
432 T moveFromTry(Try<T>& t);
435 * Throws if try contained an exception.
437 * @param t Try to move from
439 void moveFromTry(Try<void>& t);
442 * @param f a function to execute and capture the result of (value or exception)
444 * @returns Try holding the result of f
446 template <typename F>
447 typename std::enable_if<
448 !std::is_same<typename std::result_of<F()>::type, void>::value,
449 Try<typename std::result_of<F()>::type>>::type
453 * Specialization of makeTryWith for void return
455 * @param f a function to execute and capture the result of
457 * @returns Try<void> holding the result of f
459 template <typename F>
460 typename std::enable_if<
461 std::is_same<typename std::result_of<F()>::type, void>::value,
465 template <typename... Ts>
466 std::tuple<Ts...> unwrapTryTuple(std::tuple<folly::Try<Ts>...>&& ts);
470 #include <folly/Try-inl.h>