Boost logo

Boost Users :

Subject: [Boost-users] Throwing an exception in a packaged_task: is there a clear example?
From: Mark Wilson (mwil_at_[hidden])
Date: 2010-08-19 11:54:22


Hello,

I am throwing an exception from a packaged_task, and then trying to catch it
with f.get() (where f is a unique_future); it doesn't work, the program just
terminates with:

terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::unknown_exception>'
  what(): std::exception

Is there a simple example somewhere, or could someone provide one, that
makes clear how this mechanism is supposed to work? The Threads
documentation does not show how to do this...

Here is my simple program, based on the example in the docs:

#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include <boost/exception/all.hpp>

struct MyException
{
    MyException()
    {
    }
};

int calculate_the_answer_to_life_the_universe_and_everything()
{
    throw MyException();
    return 42;
}

int main()
{
    boost::packaged_task<int>
        task(calculate_the_answer_to_life_the_universe_and_everything);
    boost::unique_future<int> f(task.get_future());

    boost::thread t(boost::move(task));

    try
    {
        f.get();
    }
    catch(MyException& ex)
    {
        std::cout << "EXCEPTION!" << std::endl;
    }
}

Thanks,
Mark Wilson



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net