How to get an exception message in the future: future?

If I use Boost futures and future true reports for has_exception (), is there a way to get this 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 instead of "?????"?

+6
source share
1 answer

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) {/*....*/} ... 
+7
source

Source: https://habr.com/ru/post/892978/


All Articles