Throwing exceptions can be a convenient way to handle errors. Storing exceptions in an exception_ptr makes it easy to handle exceptions in a different thread or at a later time. exception_ptr can also be used in a very generic result/exception wrapper.
Declared in <folly/ExceptionWrapper.h>
class exception_wrapper final
However, inspecting exceptions through the exception_ptr interface, namely through rethrow_exception, is expensive. This is a wrapper interface which offers faster inspection.
exception_wrapper globalExceptionWrapper;
// Thread1
void doSomethingCrazy() {
int rc = doSomethingCrazyWithLameReturnCodes();
if (rc == NAILED_IT) {
globalExceptionWrapper = exception_wrapper();
} else if (rc == FACE_PLANT) {
globalExceptionWrapper = make_exception_wrapper<FacePlantException>();
} else if (rc == FAIL_WHALE) {
globalExceptionWrapper = make_exception_wrapper<FailWhaleException>();
}
}
// Thread2: Exceptions are ok!
void processResult() {
try {
globalExceptionWrapper.throw_exception();
} catch (const FacePlantException& e) {
LOG(ERROR) << "FACEPLANT!";
} catch (const FailWhaleException& e) {
LOG(ERROR) << "FAILWHALE!";
}
}
// Thread2: Exceptions are bad!
void processResult() {
globalExceptionWrapper.handle(
[&](FacePlantException& faceplant) {
LOG(ERROR) << "FACEPLANT";
},
[&](FailWhaleException& failwhale) {
LOG(ERROR) << "FAILWHALE!";
},
[](...) {
LOG(FATAL) << "Unrecognized exception";
});
}
| Name | Description |
|---|---|
exception_wrapper [constructor] | Constructors |
operator= | Assignment operators |
class_name | Returns the class name of the wrapped exception. |
exception_ptr | exception_ptr overloads |
get_exception | get_exception overloads |
get_mutable_exception | get_mutable_exception overloads |
handle | Handle the wrapped expression as if with a series of catch clauses, propagating the exception if no handler matches. |
has_exception_ptr | Reports whether an exception pointer is held. |
is_compatible_with | Reports whether the wrapped exception matches a given catch clause. |
reset | Make this exception_wrapper empty |
swap | Swaps the value of *this with the value of that |
terminate_with [noreturn] | Terminates the process with the wrapped expression. |
throw_exception [noreturn] | Throws the wrapped expression. |
throw_with_nested [noreturn] | Throws the wrapped expression nested into another exception. |
to_exception_ptr | to_exception_ptr overloads |
type | Returns the typeid of the wrapped exception object. If there is no wrapped exception object, returns nullptr. |
what | Returns a description of the wrapped exception. |
with_exception | Call fn with the wrapped exception (if any), if fn can accept it. |
operator bool | Reports whether this wrapper holds an exception. |
operator! | Reports whether this wrapper is empty. |
| Name | Description |
|---|---|
folly::operator== | Compares two wrappers for pointing to the same exception object. |
| Name | Description |
|---|---|
current_exception_wrapper | A convenience shorthand for exception_wrapper(current_exception()). |
exceptionStr | Returns a string describing the exception held by ew. |
make_exception_wrapper | Builds an exception_wrapper wrapping a newly constructed exception. |
swap | Swaps the value of a with the value of b. |
try_and_catch | try_and_catch is a convenience for try {} catch(...) {}` that returns an exception_wrapper with the thrown exception, if any. |
coro::makeErrorTask | Make a Task that will trivially yield an Exception. |
coro::makeFuture | Creates a future already fulfilled with the given exception. |
coro::make_error_now_task | Make a now_task that will trivially yield an exception. |