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


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

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

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

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

    catch(MyException& ex)
        std::cout << "EXCEPTION!" << std::endl;

Mark Wilson

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at