towi
towi

Reputation: 22267

How to get the argument for promise::set_exception(x)?

I found in several places on how a promise should be used references to copy_exception, but I can not find it in the current FDIS. Is there an alternative way on how to use set_exception() since those blogs?

For example here

void asyncFun(promise<int> intPromise)
{
    int result;
    try {
        // calculate the result
        intPromise.set_value(result);
    } catch (MyException e) {
        intPromise.set_exception(std::copy_exception(e));  // <- copy
    }
}

I find std::current_exception() here.

catch(...)
{
    p.set_exception(std::current_exception());
}

Therefore my questions:

Upvotes: 12

Views: 2701

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 218710

There is a different name for copy_exception. copy_exception was renamed late in the standardization process over confusion of what it actually did:

template<class E>
   exception_ptr make_exception_ptr(E e) noexcept;

Effects: Creates an exception_ptr object that refers to a copy of e, ...

Use of either make_exception_ptr or current_exception is fine, depending on what exception you're trying to set.

Upvotes: 15

Related Questions