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.
Synopsis
Declared in <folly/ExceptionWrapper.h>
class exception_wrapper final
Description
However, inspecting exceptions through the exception_ptr interface, namely through rethrow_exception, is expensive. This is a wrapper interface which offers faster inspection.
Example usage:
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";
});
}
Member Functions
Name |
Description |
|
Constructors |
Assignment operators |
|
Returns the class name of the wrapped exception. |
|
|
|
|
|
|
|
Handle the wrapped expression as if with a series of |
|
Reports whether an exception pointer is held. |
|
Reports whether the wrapped exception matches a given catch clause. |
|
Make this |
|
Swaps the value of |
|
|
Terminates the process with the wrapped expression. |
|
Throws the wrapped expression. |
|
Throws the wrapped expression nested into another exception. |
|
|
Returns the |
|
Returns a description of the wrapped exception. |
|
Call |
|
Reports whether this wrapper holds an exception. |
|
Reports whether this wrapper is empty. |
Friends
Name |
Description |
Compares two wrappers for pointing to the same exception object. |
Non-Member Functions
Name |
Description |
A convenience shorthand for |
|
Returns a string describing the exception held by |
|
Builds an |
|
Swaps the value of |
|
|
|
Make a Task that will trivially yield an Exception. |
|
Creates a future already fulfilled with the given exception. |
|
Make a |
Created with MrDocs