C++C++11C++14C++17C++20Learn C++

How To Use Propagating Exceptions In Modern C++?

How To Use Propagating Exceptions In Modern C++

Modern C++ has many features to aid multi-thread programming that allow applications to be faster and more responsive. The C++11 standard offers the possibility of moving an exception from one thread to another. This type of movement is called propagating exceptions, exception propagation; also known as rethrow exception in multi-threading. To do that, some modifications have been made to the <exception> header in C++ and there is a nullable pointer-like type std::exception_ptr. In this post, we explain std::exception_ptr and how to use the rethrow exception method in modern C++.

What are propagating exceptions (exception propagation) in modern C++?

The C++11 standard offers a nullable pointer-like type std::exception_ptr. The exception_ptr is used to refer to an exception and manages an exception object that has been thrown and captured with std::current_exception(). Here is how we can use both:

In modern C++, a concurrency support library is designed to solve problems in multi-thread operations. This library includes built-in support for threads (std::thread), atomic operations (std::atomic), mutual exclusion (std::mutex), condition variables (std::condition_variable), and many other features. In addition to these useful features, std::exception_ptr is useful in exception handling between threads. They may be passed to another function, possibly to another thread function, so that the exception can be rethrown and handled in another thread with a catch clause.

According to open-std, “An exception_ptr that refers to the currently handled exception or a copy of the currently handled exception, or a null exception_ptr if no exception is being handled. If the function needs to allocate memory and the attempt fails, it returns an exception_ptr that refers to an instance of bad_alloc. It is unspecified whether the return values of two successive calls to current_exception refer to the same exception.”

The exception_ptr can be DefaultConstructible, CopyConstructible, Assignable and EqualityComparable. By default, it is constructed as a null pointer, doesn’t point to an exception, and operations from exception_ptr do not throw.

Is there a simple example of how to use propagating exceptions in modern C++?

Here is a simple example that outputs exceptions coming from a thread.

As you see, here we used mutex m variable to lock() and unlock() printing out exceptions. Thus, we can see exceptions in order.

Is there a full example of how to use propagating exceptions in modern C++?

Here is a very good example where we use lambdas and threads to throw exceptions and to define a string s which is empty. The mylambda construct is trying to reach the 5th character and that will throw an exception. We simulate 2 threads throwing same exceptions.

For more information about the Propagating Exceptions feature, please see Propagating Exceptions Proposal document.

Exceptions are one of the most important parts of error handling methods. If you want to learn more about exceptions here are more detailed documents.

How To Use Propagating Exceptions In Modern C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++