Search code examples
c++arduinog++platformio

How Do I Enable Error Throwing In PlatformIO? (I'm getting g++ errors.)


I am trying to throw errors in my Arduino Ethernet code (if (error) throw error) and handle (really ignore: catch(Error error) {}) them. Sadly, I get errors like:

src\main.cpp: In function 'void loop()':
src\main.cpp:29:35: error: exception handling disabled, use -fexceptions to enable
     } catch (Error error) {
                    ^

I read that I need to add the following to my platformio.ini file to enable error handling:

build_flags = -fexceptions
build_unflags = -fno-exceptions

Now on the compilation, I get this:

<artificial>:(.text+0xaf8): undefined reference to `__cxa_allocate_exception'
<artificial>:(.text+0xb0a): undefined reference to `__cxa_throw'
...
<artificial>:(.text.startup+0x5c): undefined reference to `__gxx_personality_sj0'
<artificial>:(.text.startup+0x5e): undefined reference to `__gxx_personality_sj0'
<artificial>:(.text.startup+0x116): undefined reference to `setup'
<artificial>:(.text.startup+0x1fa): undefined reference to `__cxa_begin_catch'
<artificial>:(.text.startup+0x1fe): undefined reference to `__cxa_end_catch'

What should I do?


Solution

  • I suppose the AVR architecture (avr-g++). As you can see in its FAQ, it's not supported:

    Exceptions are not supported. Since exceptions are enabled by default in the C++ frontend, they explicitly need to be turned off using -fno-exceptions in the compiler options. Failing this, the linker will complain about an undefined external reference to __gxx_personality_sj0.

    It's there just because of common c++ frontend and because exceptions are enabled by default.

    However for example in the STM32duino (STM32 Arm MCUs) it's possible to use -fexceptions (as well as STL and many more)