Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Should I always use current_exception(), even when I do not catch "..."?
  • Or is there new a different name for copy_exception?
like image 763
towi Avatar asked Sep 25 '11 12:09

towi


1 Answers

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.

like image 161
Howard Hinnant Avatar answered Sep 19 '22 20:09

Howard Hinnant