Okay to use exceptions on Atmel sam3x8e?

Yesterday I wrote a function that returned the value of a byte:

char unsigned Func(void)
{
    return 'K';
}

The byte was gotten from hardware -- which could fail with an error -- and so in order to accommodate the error state, I would have done:

std::optional<char unsigned> Func(void)
{
    char unsigned val;
 
    if ( GetFromHardware(&val) ) return val;

    return std::nullopt;
}

but std::optional is only available from C++17 onwards.

So instead I decided to go with:

char unsigned Func(void)
{
    char unsigned val;
 
    if ( GetFromHardware(&val) ) throw -1;

    return std::nullopt;
}

And so then in the calling function I had:

char unsigned c;

try { c = Func(); } catch(int) { return; }

CallAnotherFunction(c);

When I tried to compile this, my compiler told me that I needed to add '-fexceptions' at the command line. So I added the compiler option and it compiled fine.

Is there any reason why I should shy away from using exceptions on the sam3x8e microcontroller? Will a thrown exception reliably unwind the stack and call destructors in the correct order?

Obviously I'm in trouble if an exception propagates all the way up past 'setup' or 'loop', but if I make sure that this doesn't happen then is it okay to use exceptions?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.