cuda::experimental::stf::on_throw#

template<class _Reaction>
auto cuda::experimental::stf::on_throw(
_Reaction &&__reaction,
const ::cuda::std::source_location __loc = ::cuda::std::source_location::current()
)#

Creates a policy saying how to react if a callable throws.

Apply the policy with on_throw(reaction) << callable, which evaluates to the result of the callable when nothing goes wrong. The reaction is one of four things, recognized by type:

  • A handler: any noexcept callable accepting a const std::exception* and a cuda::std::source_location, be it a function, a function pointer, or a function object, capturing or not. It is invoked with the caught exception, or with nullptr for an exception that does not derive from std::exception, along with __loc. Its return type says what happens next: returning decltype(::std::ignore) resumes execution with a default-constructed result, and returning void claims the handler ends the program, with std::abort running right after it in case it does not. notify is such a handler.

  • A terminating action: anything convertible to void (*)(), std::abort and std::terminate being the obvious ones. The exception is reported through notify, the action runs, and std::abort follows in case it returns.

  • std::ignore, which suppresses the exception silently and resumes execution with a default-constructed result.

  • Anything else, taken as a replacement value for the result. It must be convertible to the callable’s result type, and is moved into the result if the policy owns it.

The two resuming reactions leave a void result alone and require a default-constructible type of a non-void one, having nothing to refer to for a reference.

The callable itself must not be noexcept: an exception raised inside one ends the program where it stands, leaving the reaction unreachable, so such a pairing is rejected instead of standing there looking like protection. Call such a callable directly.

A callable returning a reference therefore goes with a terminating action, which never has to produce a result, or with a replacement passed as an lvalue of the same type, which the policy refers to rather than copies and which the caller keeps alive:

int fallback = 42;
int& x = on_throw(fallback) << [] { return returns_a_reference(); }; // x is fallback on a throw
Parameters:
  • __reaction[in] The reaction, which the policy owns if passed an rvalue and refers to if passed an lvalue.

  • __loc[in] The location passed to the handler; defaults to the call site.

Returns:

A policy object consumed by operator<<.