#include #include #include #include #include using namespace std ; void worker_with_promise(promise&& promise_obj) { try { // Simulate work and an error throw runtime_error("Error occurred in manual thread management."); // If no exception, you could fulfill the promise like this: // promise_obj.set_value(); } catch (...) { // Catch the exception within the worker thread and store it in the promise promise_obj.set_exception(current_exception()); } } int main() { promise promise_obj; future future_obj = promise_obj.get_future(); thread th(worker_with_promise, move(promise_obj)); try { // Wait for the result/exception future_obj.get(); cout << "Thread finished successfully (should not happen in this example)." << endl; } catch (const runtime_error& e) { cerr << "Caught exception in main: " << e.what() << endl; } th.join(); // Clean up the thread return 0; }