Unable to handle exception on Adafruit ItsyBitsy M0

Hello,

In this simple program, I try to catch and handle a dummy exception on the Adafruit ItsyBitsy M0.

I am using Visual Studio Code with PlatformIO.

Visual Studio Code version 1.68.1
PlatformIO Core version 6.1.4
PlatformIO Home version 3.4.3

Below is the platformio.ini and main.cpp source file used.

main.cpp

#include <Arduino.h>

#include <stdexcept>  // including only <exception> leaves std::runtime_error undefined

void setup() {
    Serial.begin(9600);
    while (!Serial) {
        delay(100);  // wait for the serial port to connect
    }

    Serial.println("Start.");
    try {
        throw std::runtime_error("dummy exception");  // trigger a catch
    } catch (const std::exception& e) {
        Serial.println(e.what());
    }
    Serial.println("End.");
}

void loop() {
    // Do nothing.
}

platformio.ini

[env:adafruit_itsybitsy_m0]
platform = atmelsam
board = adafruit_itsybitsy_m0
framework = arduino

Without the build flag -fexceptions, the code would not compile:

src\main.cpp: In function 'void setup()':
src\main.cpp:13:51: error: exception handling disabled, use '-fexceptions' to enable
   13 |         throw std::runtime_error("dummy exception");

After that was added, the code compiled. The file is now:

platformio.ini

[env:adafruit_itsybitsy_m0]
platform = atmelsam
board = adafruit_itsybitsy_m0
framework = arduino
build_flags =
  -fexceptions

However, the program output did not match my expectation.

Expected output:

Start.
dummy exception
End.

Actual output:

Start.

From that, you can see the exception is not handled, and the program stops without printing the last print statement.

Seeing as I could not find a solution to the problem online. I gave solutions to other boards a try, among others: how to enable exceptions on ESP8266. And added the build unflag -fno-exceptions.

The complete platformio.ini file is now:

platformio.ini

[env:adafruit_itsybitsy_m0]
platform = atmelsam
board = adafruit_itsybitsy_m0
framework = arduino
build_flags =
  -fexceptions
build_unflags = 
  -fno-exceptions

However, the output remains the same and the problem is not solved.

Other things I have tried is add the build flags -lstdc++ and -lsupc++. No luck.
Inspired by this issue, added the unbuild flag --specs=nano.specs, again, no luck.

On the similar board SparkFun Electronics SAMD21 Mini Breakout, that uses the same microcontroller (ATSAMD21G18 32-bit Cortex M0+) , I am able to handle the exception as intended.

main.cpp

#include <Arduino.h>

#undef max  // conflicts with standard library
#undef min  // conflicts with standard library

#include <stdexcept>  // including only <exception> leaves std::runtime_error undefined

void setup() {
    SerialUSB.begin(9600);
    while (!SerialUSB) {
        delay(100);  // wait for the serial port to connect
    }

    SerialUSB.println("Start.");
    try {
        throw std::runtime_error("dummy exception");  // trigger a catch
    } catch (const std::exception& e) {
        SerialUSB.println(e.what());
    }
    SerialUSB.println("End.");
}

void loop() {
    // Do nothing.
}

platformio.ini

[env:sparkfun_samd21_mini_usb]
platform = atmelsam
board = sparkfun_samd21_mini_usb
framework = arduino
build_flags =
  -fexceptions
build_unflags =
  -fno-exceptions
  --specs=nano.specs

Because it is possible on the SAMD21 Mini Breakout, it leads me to believe it is possible on the ItsyBitsy M0 too.

How can I successfully handle an exception on the Adafruit ItsyBitsy M0?

Thank you in advance.

Good question. I think --specs=nano.specs can’t be removed purely using build_unflags in the env["LINKFLAGS"], it may need a little extra_script. I’ll look into it.

1 Like

Thank you for the swift reply, and that you are looking into it.

Sorry to bother you, has there been any development on this matter?

Thank you in advance.

Right, I’ll look into it