Book Image

Learning Boost C++ Libraries

By : Arindam Mukherjee
Book Image

Learning Boost C++ Libraries

By: Arindam Mukherjee

Overview of this book

Table of Contents (19 chapters)
Learning Boost C++ Libraries
About the Author
About the Reviewers

C++11 exception-handling improvements

C++11 introduced the ability to capture and store an exception that can be passed around and rethrown later. This is particularly useful for propagating exceptions across threads.

Storing and rethrowing exceptions

To store an exception, the type std::exception_ptr is used. std::exception_ptr is a smart pointer type with shared ownership semantics, not unlike std::shared_ptr (see Chapter 3, Memory Management and Exception Safety). An instance of std::exception_ptr is copyable and movable and can be passed to other functions potentially across threads. A default-constructed std::exception_ptr is a null object that does not point to any exception. Copying a std::exception_ptr object creates two instances that manage the same underlying exception object. The underlying exception object continues to exist as long as the last exception_ptr instance containing it exists.

The function std::current_exception, when called inside a catch-block, returns the active exception for which the catch-block was executed, wrapped in an instance of std::exception_ptr. When called outside a catch-block, it returns a null std::exception_ptr instance.

The function std::rethrow_exception is passed an instance of std::exception_ptr (which must not be null) and throws the exception contained in the std::exception_ptr instance.

Listing A.3: Using std::exception_ptr

 1 #include <stdexcept>
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 6 void do_work()
 7 {
 8   throw std::runtime_error("Exception in do_work");
 9 }
11 std::vector<std::exception_ptr> exceptions;
13 void do_more_work()
14 {
15   std::exception_ptr eptr;
17   try {
18     do_work();
19   } catch (...) {
20     eptr = std::current_exception();
21   }
23   std::exception_ptr eptr2(eptr);
24   exceptions.push_back(eptr);
25   exceptions.push_back(eptr2);
26 }
28 int main()
29 {
30   do_more_work();
32   for (auto& eptr: exceptions) try {
33     std::rethrow_exception(eptr);
34   } catch (std::exception& e) {
35     std::cout << e.what() << '\n';
36   }
37 }

Running the preceding example prints the following:

Exception in do_work
Exception in do_work

The main function calls do_more_work (line 30), which in turn calls do_work (line 18), which simply throws a runtime_error exception (line 8) that finds its way down to a catch-block in do_more_work (line 19). We declare an object eptr of type std::exception_ptr in do_more_work (line 15) and inside the catch-block, we call std::current_exception and assign the result to eptr. Later, we create a copy of eptr (line 23), and push both instances into a global vector of exception_ptrs (lines 24-25).

In the main function, we run through the exception_ptr instances in the global vector, throw each using std::rethrow_exception (line 33), catch it and print its message. Note that in the process, we print the message from the same exception twice because we have two instances of exception_ptr containing the same exception.

Storing and rethrowing exception using Boost

In pre-C++11 environments, you can use the boost::exception_ptr type to store exceptions and boost::rethrow_exception to throw the exception stored in boost::exception_ptr. There is also the boost::current_exception function which works akin to std::current_exception. But without underlying language support, it requires help from the programmer to function.

In order for boost::current_exception to return the currently active exception wrapped in boost::exception_ptr, we must modify the exception before throwing it to make it amenable to be handled using this mechanism. To do this, we call boost::enable_current_exception on the exception to be thrown. The following snippet illustrates this:

Listing A.4: Using boost::exception_ptr

 1 #include <boost/exception_ptr.hpp>
 2 #include <iostream>
 4 void do_work()
 5 {
 6   throw boost::enable_current_exception(
 7             std::runtime_error("Exception in do_work"));
 8 }
10 void do_more_work()
11 {
12   boost::exception_ptr eptr;
14   try {
15     do_work();
16   } catch (...) {
17     eptr = boost::current_exception();
18   }
20   boost::rethrow_exception(eptr);
21 }
23 int main() {
24   try {
25     do_more_work();
26   } catch (std::exception& e) {
27     std::cout << e.what() << '\n';
28   }
29 }