exception wrapper
authorMarc Celani <marccelani@fb.com>
Thu, 1 May 2014 17:44:13 +0000 (10:44 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 20 May 2014 19:53:57 +0000 (12:53 -0700)
commitad55cd0ecec958879f835e6535eaa08d6028c376
tree638869e3db57c758c2b4d014e6ae273ecc362782
parent6b20f11e8c85252e856aeb06dc89d53f5b66c59c
exception wrapper

Summary:
folly::exception_wrapper is a different take on std::exception_ptr to
suit a specific use case.

The good: std::exception_ptr is not templated, so it can easily be used in
different classes without template creep. You can pass errors around between
threads or simply between modules. Rethrowing the exception throws the *proper*
derived class exception, not some base class.

The bad: Getting access to the exception requires throwing, which is expensive.
Many users of popular frameworks that take advantage of std::exception_ptr
check if the exception is set, and if so do some error handling without actually
knowing the type of the exception or logging its message, just to avoid the cost
of rethrowing the exception.

The ugly: Creating an exception_ptr requires throwing the exception as least
once. This is bad in the performance sensitive case where users will not even
inspect the exception.

This class takes advantage of the good while avoiding the requirement to throw.
By using a templated deleter and thrower function, we can create an
exception_wrapper which is properly managed, can be thrown, and can be retrieved
as a void* with a get() function. Users that previously caught exceptions are
now able to dynamically cast to different exception types they formerly caught
to avoid the unwind cost while still getting details about the error.

Test Plan: unit test

Reviewed By: davejwatson@fb.com

FB internal diff: D1305470

@override-unit-failures
folly/ExceptionWrapper.h [new file with mode: 0644]
folly/Makefile.am
folly/detail/ExceptionWrapper.h [new file with mode: 0644]
folly/test/ExceptionWrapperBenchmark.cpp [new file with mode: 0644]
folly/test/ExceptionWrapperTest.cpp [new file with mode: 0644]