Reducing Binary sizes in Embedded Systems using C++ Exceptions

By Khalil Estell

Universally, exceptions are avoided in embedded systems C++ code, with a preference for error codes as out parameters, return values, or sum types like std::expected<T,E>. This avoidance stems from two main issues: the size of the exception handling runtime and the performance cost of throwing an exception versus returning a status code. But what if I were to tell you that exceptions are the better choice for producing smaller binaries than using the alternatives?

In this talk, I will explore the space cost of exceptions on a Cortex M4 microcontroller using the ARM GNU Toolchain, how to reduce the space cost, compare it to result types and identifying the point where result types become more costly in terms of code size than exceptions. I will delve into the performance of exceptions on the same Cortex M4 microcontroller, demonstrating how I managed to cut the cycle count in half. I’ll offer suggestions for improving exception performance on the sad path and explain how exceptions enhance performance on the happy path. Finally, I’ll share why, after 11 years in embedded systems, 7 years teaching embedded systems, and 6 years in professional consumer electronics, I now believe that exceptions are the superior error handling mechanism for embedded systems.





Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.