O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exception handling with the concurrency library

The concurrency support library also supports passing exceptions via a future object.  

Let's understand the exception concurrency handling mechanism with a simple example, as follows:

#include <iostream>#include <future>#include <promise>using namespace std; void add ( int firstInput, int secondInput, promise<int> output ) {  try {         if ( ( INT_MAX == firstInput ) || ( INT_MAX == secondInput ) )             output.set_exception( current_exception() ) ;        }  catch(...) {}       output.set_value( firstInput + secondInput ) ;}int main ( ) {     try {    promise<int> promise_;          future<int> output = promise_.get_future();    async ( launch::deferred, add, INT_MAX, INT_MAX, move(promise_) ); cout << "The sum of INT_MAX + INT_MAX is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required