1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
In practice almost everything can be used as an exception. But at the same
time any exception that is thrown can be reached using the function
hi(current_exception)(tt(std::current_exception)), and access to any exception
can be standardized using hi(make_exception_ptr)tt(std::make_exception_ptr).
These functions expect or use objects of the class
hi(exception_ptr)tt(std::exception_ptr), and in this section we take a closer
look at tha class.
The class tt(exception_ptr's) default constructor initializes it to a
null-pointer. In the following code snippet the variable tt(isNull) is set to
true:
verb( std::exception_ptr obj;
bool isNull = obj == nullptr && obj == 0;)
The class tt(exception_ptr) provides copy and move constructors as well as
copy and move assignment operators.
Two tt(exception_ptr) objects can be compared for equality. They are equal
if they refer to the same exception. Move assignment transfers the exception
referred to by the right-hand side operand to the left-hand side operand, and
turns the right-hand side operand into a null pointer.
There is no published method directly retrieving the exception to which an
tt(exception_ptr) object refers. However, there are some free functions
constructing or handling tt(exception_ptr) objects:
itemization(
ithtq(current_exception)
(std::exception_ptr std::current_exception() noexcept)
(An tt(exception_ptr) object is returned referring to the currently
handled exception (or a copy of the currently handled exception, or a
default constructed tt(exception_ptr) object if no current exception
is available). This function can also be called when a default
exception catcher is used.
COMMENT(
E.g., assuming that tt(obj) refers to an
available tt(std::promise) object, then the following code snippet
assigns the exception caught by default catch clause to tt(obj):
verb( ...
catch (...)
{
obj.set_exception(std::current_exception());
})
)
The exception referred to by tt(current_exception) does not have to
be an object of the class tt(std::exception). Any type of object or
value thrown as an exception is retrieved as an tt(exception_ptr) by
tt(current_exception). The exception referred to by an
tt(exception_ptr) object remains valid for at least as long as there
exists an tt(exception_ptr) object that refers to it. Calling
tt(current_exception) twice in a row then the two returned
tt(exception_ptr) objects may or may not refer to the same exception
object.)
ithtq(make_exception_ptr)(std::exception_ptr make_exception_ptr(Type
value) noexcept)
(This function template constructs an tt(exception_ptr) from a value of
any type which is passed as its argument. tt(Type) does not
necessarily have to be a tt(std::exception) but can be anything that
can be thrown as an exception: an tt(int), a tt(std::string), a
tt(std::exception), you name it.
COMMENT(
The constructed
tt(exception_ptr) could, e.g., be assigned to a tt(std::promise). When
the promise's future's tt(get) member is subsequently called (possibly
from within another thread) the exception will be thrown.
END)
Here are
some examples, showing how values of different types can be passed as
arguments to tt(make_exception_ptr)
COMMENT(
, and showing how the eventually
constructed tt(exception_ptr) is assigned to the tt(obj), which is
assumed to be of a tt(std::promise) type:
END)
verb( auto ptr = make_exception_ptr(exception());
ptr = make_exception("hello world"s);
ptr = make_exception(12);)
COMMENT(
obj.set_exception(make_exception_ptr(ptr));
END)
)
ithtq(rethrow_exception)(void std::rethrow_exception(exception_ptr obj))
(The exception to which tt(obj) refers is thrown. Note: tt(obj)
cannot be a tt(nullptr).)
)
|