If you can’t throw an exception and can’t use
finally) from the guideline support library, you have a problem. Exceptional states require exceptional actions:
I was pretty surprised to read the guidelines about
goto exit; the final rescue. Here are the remaining rules for error handling in the C++ core guidelines.
- E.25: If you can’t throw exceptions, simulate RAII for resource management
- E.26: If you can’t throw exceptions, consider failing fast
- E.27: If you can’t throw exceptions, use error codes systematically
- E.30: Don’t use exception specifications
- E.31: Properly order your
The first three rules are quite related; therefore, I will write about them together.
E5: If you can’t throw exceptions, simulate RAII for resource management, E.26: If you can’t throw exceptions, consider failing fast, and E.27: If you can’t throw exceptions, use error codes systematically
The idea of RAII is quite simple. If you have to take care of a resource, put the resource into a class
. Use the class’s constructor for the initialization and the destructor for the destruction of the resource. When you create a local instance of the class on the stack, the C++ runtime takes care of the resource, and you are done. For more information on RAII, read my previous post Garbage Collection – No Thanks.
What does it mean to simulate RAII for resource management? Imagine you have a function
func which exists with an exception if G
adget can’t be created.
If you can not throw an exception, you should simulate RAII by adding a
valid method to
In this case, the caller has to test the return value.
Rules E.26 is straightforward. If there is no way to recover from an error such as memory exhaustion, fail fast. If you can’t throw an exception call
std::abort that causes abnormal program termination.
std::abort will only cause an abnormal program termination if you don’t install a signal handler that catches the signal SIGABRT.
The function f behaves such as the following function:
Now, I will write about the non-word
goto in rule E.27.
In case of an error, you have a few issues to solve according to the guidelines:
- how do you transmit an error indicator from out of a function?
- how do you release all resources from a function before doing an error exit?
- What do you use as an error indicator?
In general, your function should have two return values. The value and the error indicator, therefore,
std::pair is a good fit. Releasing the resources may quickly become a maintenance nightmare, even if you encapsulate the cleanup code in functions.
Okay, that seems to be correct! Or?
Do you know what DRY stands for? Don’t Repeat Yourself. Although the cleanup code is encapsulated into functions the code has a smell of code repetition because the cleanup functions are invoked in various places. How can we get rid of repetition? Just put the cleanup code at the end of the function and jump to it.
Admitted, with the help of
goto the overall structure of the function is quite clear. Just the error indicator (1) is set in case of an error. Exceptional states require exceptional actions.
First, here is an example of an exception specification:
This means that the function used may allow throwing an exception of type
Y. If a different exception is thrown,
std::terminate it is called.
Dynamic exception specification with argument
throw(X, Y) and without argument
throw() is deprecated since C++11. Dynamic exception specification with arguments is removed with C++17, but dynamic exception specification without arguments will be removed with C++20. th
row() is equivalent to
noexcept. Here are more details: C++ Core Guidelines: The noexcept Specifier and Operator.
If you don’t know the last rule, it can be astonishing.
An exception is cached according to the best-fit strategy. This means the first exception handler that fits an actual exception is used. This is why you should structure your exception handler from specific to general. If not, your specific exception handler may never be invoked. In the following example, the
DivisionByZeroException is derived from
In this case, the
DivisionByZeroException (2) is used first for handling the exception thrown in line (1). If the specific handler does not work, all exceptions derived from
std::exception (3) are caught in the following line. The last exception handler has an ellipsis (4) and can, therefore, catch all exceptions.
As promised, I will write in the next post about the five rules for constants and immutability in C++.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,