Site icon Learn 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:

[crayon-6763b3f6a1fda570302578/]

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.

[crayon-6763b3f6a1fe3252357676/]

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.

[crayon-6763b3f6a1fe9060927777/]

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.

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.

Exit mobile version