Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Boost C++ Libraries

You're reading from  Learning Boost C++ Libraries

Product type Book
Published in Jul 2015
Publisher
ISBN-13 9781783551217
Pages 558 pages
Edition 1st Edition
Languages
Author (1):
Arindam Mukherjee Arindam Mukherjee
Profile icon Arindam Mukherjee

Table of Contents (19) Chapters

Learning Boost C++ Libraries
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Introducing Boost 2. The First Brush with Boost's Utilities 3. Memory Management and Exception Safety 4. Working with Strings 5. Effective Data Structures beyond STL 6. Bimap and Multi-index Containers 7. Higher Order and Compile-time Programming 8. Date and Time Libraries 9. Files, Directories, and IOStreams 10. Concurrency with Boost 11. Network Programming Using Boost Asio C++11 Language Features Emulation Index

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>
 5
 6 void do_work()
 7 {
 8   throw std::runtime_error("Exception in do_work");
 9 }
10
11 std::vector<std::exception_ptr> exceptions;
12
13 void do_more_work()
14 {
15   std::exception_ptr eptr;
16
17   try {
18     do_work();
19   } catch (...) {
20     eptr = std::current_exception();
21   }
22
23   std::exception_ptr eptr2(eptr);
24   exceptions.push_back(eptr);
25   exceptions.push_back(eptr2);
26 }
27
28 int main()
29 {
30   do_more_work();
31
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>
 3
 4 void do_work()
 5 {
 6   throw boost::enable_current_exception(
 7             std::runtime_error("Exception in do_work"));
 8 }
 9
10 void do_more_work()
11 {
12   boost::exception_ptr eptr;
13 
14   try {
15     do_work();
16   } catch (...) {
17     eptr = boost::current_exception();
18   }
19
20   boost::rethrow_exception(eptr);
21 }
22
23 int main() {
24   try {
25     do_more_work();
26   } catch (std::exception& e) {
27     std::cout << e.what() << '\n';
28   }
29 }
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}