return e_;
}
+ /*
+ * @returns a pointer to the `std::exception` held by `*this`, if one is held;
+ * otherwise, returns `nullptr`.
+ */
+ std::exception* tryGetExceptionObject() {
+ return hasException() ? e_.get_exception() : nullptr;
+ }
+ std::exception const* tryGetExceptionObject() const {
+ return hasException() ? e_.get_exception() : nullptr;
+ }
+
+ /*
+ * @returns a pointer to the `Ex` held by `*this`, if it holds an object whose
+ * type `From` permits `std::is_convertible<From*, Ex*>`; otherwise,
+ * returns `nullptr`.
+ */
+ template <class E>
+ E* tryGetExceptionObject() {
+ return hasException() ? e_.get_exception<E>() : nullptr;
+ }
+ template <class E>
+ E const* tryGetExceptionObject() const {
+ return hasException() ? e_.get_exception<E>() : nullptr;
+ }
+
/*
* If the Try contains an exception and it is of type Ex, execute func(Ex)
*
return e_;
}
+ /*
+ * @returns a pointer to the `std::exception` held by `*this`, if one is held;
+ * otherwise, returns `nullptr`.
+ */
+ std::exception* tryGetExceptionObject() {
+ return hasException() ? e_.get_exception() : nullptr;
+ }
+ std::exception const* tryGetExceptionObject() const {
+ return hasException() ? e_.get_exception() : nullptr;
+ }
+
+ /*
+ * @returns a pointer to the `Ex` held by `*this`, if it holds an object whose
+ * type `From` permits `std::is_convertible<From*, Ex*>`; otherwise,
+ * returns `nullptr`.
+ */
+ template <class E>
+ E* tryGetExceptionObject() {
+ return hasException() ? e_.get_exception<E>() : nullptr;
+ }
+ template <class E>
+ E const* tryGetExceptionObject() const {
+ return hasException() ? e_.get_exception<E>() : nullptr;
+ }
+
/*
* If the Try contains an exception and it is of type Ex, execute func(Ex)
*
* limitations under the License.
*/
-#include <folly/Memory.h>
#include <folly/Try.h>
+
+#include <glog/logging.h>
+
+#include <folly/Memory.h>
#include <folly/portability/GTest.h>
using namespace folly;
auto result = makeTryWith(func);
EXPECT_TRUE(result.hasException<std::runtime_error>());
}
+
+template <typename E>
+static E* get_exception(std::exception_ptr eptr) {
+ try {
+ std::rethrow_exception(eptr);
+ } catch (E& e) {
+ return &e;
+ } catch (...) {
+ return nullptr;
+ }
+}
+
+TEST(Try, tryGetExceptionObject) {
+ auto epexn = std::make_exception_ptr(std::range_error("oops"));
+ auto epnum = std::make_exception_ptr(17);
+
+ auto exn = CHECK_NOTNULL(get_exception<std::range_error>(epexn));
+ auto num = CHECK_NOTNULL(get_exception<int>(epnum));
+
+ {
+ auto t = Try<bool>(true);
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<bool>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<bool>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<void>();
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<void>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<void>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<bool>(true);
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<bool>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<bool>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<void>();
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<void>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<void>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+}