Exception handling disabled, use -fexceptions to enable

Have the following error:

src/main.cpp: In function ‘void testFoo()’:
src/main.cpp:5:9: error: exception handling disabled, use -fexceptions to enable
throw “Test exception”;

My platformio.ini:
[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino
build_flags = -fexceptions

Test source:
#include <Arduino.h>

void testFoo(void)
{
throw “Test exception”;
}
void setup()
{
}
void loop()
{
}

Core version 4.0.0, Home 2.2.0

Please help

Doing a verbose build indicates that -fexceptions is correctly being passed to the compiler, but -fno-exceptions is also passed by default, so that needs to be turned off. To unset that flag, use build_unflags.

[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino
build_flags = -fexceptions
build_unflags = -fno-exceptions
3 Likes

Thank you! It works. Would be nice to cover this in the docs as well, or at least include this in the error message. Also, it’s unclear what for is it done - I mean why this flag is set by default

1 Like

Fair point… since enabling exceptions is exposed as a menu item in the ESP8266 Arduino core… should probably be added as an actual flag to be set. Actually no, just how it is now… simplest way, and it’s consistent with the other parameters… Edit: I’ve just done a PR to get that added : Document how to enable exceptions on ESP8266 by pfeerick · Pull Request #69 · platformio/platformio-docs · GitHub

As far as why it’s set by default, I would suspect because that’s the default value of the ESP8266 Arduino core…

OK, now I have exceptions enabled. But when I throw one and try to catch and handle it I got scary dump message:

User exception (panic/abort/assert)
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Abort called

From what I have googled around I am not sure if it is possible to use exceptions with ESP8266 with Arduino platform. But why would you solve problems with exceptions flags, when the exceptions mechanism would not be working?

EDIT: Interesting, in Arduino IDE when Tools->Exceptions set to Enable the exception is properly caught and handled.

My testing code;

#include <Arduino.h>
#include <iostream>
#include <exception>

using namespace std; 

void func(void) {
  throw runtime_error("exception thrown.");
}

void setup() {
  Serial.begin(9600);
  delay(200);
  
  try {
    func();
  } catch (const exception& e) {
    cout << "exception caught: " << e.what() << endl;
  }
}

void loop() {
  while(true);
}

And my platform.ini file:

[env:d1_mini_lite]
platform = espressif8266
board = d1_mini_lite
framework = arduino
upload_speed = 921600
build_flags = -fexceptions
build_unflags = -fno-exceptions

does not work for me:
in platform.ini:
build_flags = -fexceptions
build_unflags = -fno-exceptions
and in platformio-build.py I changed under LIBS=[] the stdc++ to stdc+±exc
If I copy the exception message to the window from the exception decoder, there is no decoded info available

Are you sure you’re not mixing up two different things? C++ exceptions with try/catch are completely separate from the exception log or decoder that is outputted when the application crashes.

The exception decoder is builtin to PlatformIO, I don’t know where you get the “Windows from the exception decoder”. It can’t do anything too when it doesn’t know about the .elf firmware file which contains all the symbol information needed for decoding.

If you just want to decode an exception log thrown during runtime, you must build in debug mode and activate the exception log decoder. This is done by adding

build_type = debug
monitor_filters = esp32_exception_decoder

See docs and docs.

Thank you for the information, I informed me now about the try/catch functionality. Maybe this useful to find my problem

If you have a crashing firmware you can always open a new topic with the needed information (code + platformio.ini + execution log) to get help.