#include#include int main() { std::future fut = std::async([](){ return 42; }); int result = fut.get(); std::cout << "Result: " << result << std::endl; return 0; }
#includeIn this example, we define a function that can throw an exception if the second argument is 0. We execute this function in a future and try to retrieve the result. Since the division by 0 throws an exception, we catch it and print the error message. Package Library:#include #include int divide(int a, int b) { if(b == 0) { throw std::invalid_argument("Division by zero"); } return a/b; } int main() { std::future fut = std::async([](){ return divide(42, 0); }); try { int result = fut.get(); } catch(const std::exception& ex) { std::cout << "Exception caught: " << ex.what() << std::endl; } return 0; }