Arduino Due & exceptions

I found that the toolchain used to build projects for the Arduino Due supports exception handling (Arduino 1.5.2). Whether or not this is a good thing, I'm not sure yet. I'm trying to take the step from embedded development using C to C++, and I'd like to hear some thoughts about using exceptions on an embedded system like the Arduino Due.

To test this out for yourself, all you need to do is remove -fno-exceptions from the C++ compiler flags. Unfortunately this can't be done using the Arduino IDE. There seems to be plenty of alternatives out there, here's my Eclipse-based alternative: Once this is done, you can write try/catch style exception handlers:

    try {
        throw 20;
    } catch (int e) {
        Serial.print("An exception occurred. Exception Nr. "); Serial.print(e);

You can also set unhandled exception behaviour using std::set_terminate().

I'm only experienced with embedded C development and don't know too much about what's involved in handling exceptions. It seems as though it's hard to keep exception handling code out of your Arduino Due project - simply initialising a static variable can bring in the code. It takes up roughly 60kB of program space so it seems like a bit of a shame not to use it.

If I had to phrase all the above as a question, it'd go something like "Should I use exceptions on the Arduino Due, or should I avoid them and hope that a future Arduino toolchain doesn't include exception support so I have 60kB of extra space for my code?"

The 4.7-2013 Linaro bare-metal toolchain (4.7-2013-q1-update : Series 4.7 : GNU Arm Embedded Toolchain) includes Newlib-nano, built without exception support. You can choose to use this by adding --specs=nano.specs to the linker arguments.

Here are binary sizes of the Arduino blink example, with “static int i = SystemCoreClock;” added to setup():

Arduino 1.5.2 toolchain: 72016 bytes
Linaro 4.7-2013-q1 toolchain: 98584 bytes
Linaro 4.7-2013-q1 toolchain, using newlib-nano : 13860 bytes

All binaries worked on the Arduino hardware :slight_smile: