As an embedded C++ developer, I typically write programs with exceptions disabled. My typical method for accomplishing this is by using the -fno-exceptions compiler flag. Unfortunately, I never realized a very important detail: even if I compile with -fno-exceptions, the C++ libraries I’m using have not been compiled with -fno-exceptions.
In order to be truly safe under this condition, I would need to avoid any standard library functions which might throw an exception, because they still will throw even if I cannot catch.
While some compilers, such as Keil, will correctly link a C++ library variant with exceptions disabled, this behavior is not guaranteed. If you are unsure whether a different library is being linked, I recommend compiling the C++ runtime with exceptions disabled. In this case, throw calls will be replaced with abort, which should be defined appropriately for your system.
Further Reading
- GCC: Using Exceptions
- With “-fno-exceptions”, what happens with “new T”?
- The dangers of `-fno-exceptions
Using C++ Without the Heap
Want to use C++, but worried about how much it relies on dynamic memory allocations? Our course provides a hands-on approach for learning a diverse set of patterns, tools, and techniques for writing C++ code that never uses the heap.
Learn More on the Course Page
