Introduction to C++ 11 Series – Part 5, Copying & Re-throwing exceptions

March 25, 2013

Many of the new features of C++ 11 came from a real need, not just to make the language more productive but also to enable real-life scenarios. The ability to catch exception in one place in runtime environment and to re-throw it later came from the need to provide a better concurrency support in the language and libraries. C++ 11 has many new concurrent abilities that I will talk about in future posts, but now let us just say that we have, in standard C++, the ability to create and use threads. In order to understand why we need the ability to copy and re-throw an exception, let us say that we have a situation where a master thread uses a bunch of background worker threads to efficiently execute a heavy-duty algorithm. Suppose one (or more) of them encounters an error and throws an exception. Since C++ exception handling is a stack-based mechanism, we can only catch this exception within the thread that threw the exception. However, when looking at the logical call, the operation was initiated by the master thread and any exception has to be captured by it. For that we need the ability to catch the exception in the worker thread and to re-throw it in the context of the master thread.

To start using this feature one needs to include the <exception_ptr> header file. The exception_ptr class is the heart of the mechanism. An exception_ptr instance can hold a caught exception. An empty constructor represents an instance with no exception. You can use it to check a no-error condition. The current_exception function takes a copy of the current exception and the rethrow_excteption re-throws the captured exception.

Here is a sample that uses the new C++ 11 thread class and the new lambda function that shows how to use these mechanisms:

void execute_on_another_thread(function<void()> background_task)

{

       exception_ptr exp_ptr;

 

       thread t([&]()

       {

              try

              {

                     background_task();

              }

              catch(…)

              {

                     exp_ptr = current_exception();

              }

       });

 

       t.join();

      

       if (exp_ptr == exception_ptr())

              return;

      

       rethrow_exception(exp_ptr);

}

 

void exception_ptr_sample()

{

       try

       {

              execute_on_another_thread([]

              {

                     stringstream msg;

                     msg << "Error in thread id: " << this_thread::get_id();

                     throw exception(msg.str().c_str());

              });

       }

       catch (exception e)

       {

              cout << "Current thread id: " << this_thread::get_id() << endl;

              cout << e.what() << endl;

       }

}

The exception_ptr_sample function calls the execute_on_another_thread function, providing it a lambda expression function. The background_task function argument holds the lambda function. A new thread instance is created and the lambda function to be executed, within this new thread, has captured the background_task. The background_task is executed inside a try-catch block by a new thread. Looking at the original lambda function, we see that a standard C++ exception is thrown; this exception message has the current thread id. The exception is caught and copied to a captured exception_ptr instance. To know if there was an exception we check the exp_ptr with an empty exception_ptr instance. After the execution thread exits the join, (the inner thread has finished executing), it re-throws the exception, this time in the context of the calling thread. This exception is handled in the last catch block when we see the thread id of the background execution thread and the current thread id.

This is the result:

clip_image001

This mechanism was one of the first C++ 11 features implemented in Visual Studio 2010 before the C++ 11 standard release. The drive to have this feature was to implement ConcRT & PPL – the C++ Concurrent Runtime and Parallel Pattern Library.

You can read more about this feature here.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

4 comments

  1. tietrinnyMay 2, 2013 ב 14:25

    I’m impressed, I should say. Truly rarely do I encounter a blog that’s both educative and entertaining, and let me tell you, you have hit the nail on the head. Your concept is outstanding; the issue is something that not enough persons are speaking intelligently about. I’m really happy that I stumbled across this in my search for some thing relating to this.

    [url=http://cheapchinajordansc.webstarts.com/]cheap jordan shoes wholesale[/url]

    Reply
  2. peloEmegoHofeMay 4, 2013 ב 06:44

    This is the appropriate blog for anyone who desires to find out about this topic. You realize so significantly its nearly challenging to argue with you (not that I really would want?-HaHa). You absolutely put a brand new spin on a subject thats been written about for years. Terrific stuff, just excellent!

    [URL=http://www.outletredsoleshoes.com]red bottom heels[/URL]

    Reply
  3. rooleatlyMay 15, 2013 ב 19:58

    you’ve got an awesome blog here! would you like to create some invite posts on my weblog?

    [URL=http://www.lululemononforsale.com/new-products.html]lululemon sale[/URL]

    Reply
  4. Cash Advance LoanJuly 27, 2013 ב 01:49

    graceful click this link now

    Reply