Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the exception reported to boost::future?

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 "?????"?

like image 837
petersohn Avatar asked Jul 18 '11 09:07

petersohn


1 Answers

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)
{/*....*/}
...
like image 50
CharlesB Avatar answered Nov 08 '22 05:11

CharlesB