#include #include #include #include using namespace std ; // Function that runs in the worker thread and throws an exception void throw_exception_function() { cout << "Worker thread ID: " << this_thread::get_id() << endl; // Simulate some work throw runtime_error("Something went wrong in the worker thread!"); } int main() { try { // Use async to run the function asynchronously. // It returns a future object. future future_result = async(launch::async, throw_exception_function); cout << "Main thread ID: " << this_thread::get_id() << endl; cout << "Waiting for the worker thread to finish..." << endl; // Call .get() on the future. If the function threw an exception, // .get() will re-throw that exact exception in the main thread. future_result.get(); } catch (const runtime_error& e) { cerr << "Caught exception in main: " << e.what() << endl; } catch (...) { cerr << "Caught an unknown exception in main." << endl; } return 0; }