8

If I use Boost futures, and the future reports true to has_exception(), is there any way to retrieve that exception? For example, here is the following code:

int do_something() {
    ...
    throw some_exception();
    ...  
}

...

boost::packaged_task task(do_something);
boost::unique_future<int> fi=task.get_future();
boost::thread thread(boost::move(task));
fi.wait();
if (fi.has_exception()) {
    boost::rethrow_exception(?????);
}
...

The question is, what should be put in the place of "?????"?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
petersohn
  • 11,292
  • 13
  • 61
  • 98
  • Docs say for `has_exception`: `true if *this is associated with an asynchronous result, that result is ready for retrieval, and the result is a stored exception`. But this great bit of documentation doesn't say how to get it.... – CharlesB Jul 18 '11 at 09:11
  • have you tried simply `fi.get()`? – Nim Jul 18 '11 at 09:32

1 Answers1

9

According to http://groups.google.com/group/boost-list/browse_thread/thread/1340bf8190eec9d9?fwc=2, you need to do this instead:

#include <boost/throw_exception.hpp>

int do_something() {
    ...
    BOOST_THROW_EXCEPTION(some_exception());
    ...  
}

...
try
{
  boost::packaged_task task(do_something);
  boost::unique_future<int> fi=task.get_future();
  boost::thread thread(boost::move(task));
  int answer = fi.get(); 
}
catch(const some_exception&)
{ cout<< "caught some_exception" << endl;}
catch(const std::exception& err)
{/*....*/}
...
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • Thank you. Meanwhile I found the answer, looking at the source code. Actually, I found it written in the documentation, in a sufficiently well-hidden way. – petersohn Jul 18 '11 at 09:48