try/catch block in Arduino

The Arduino reference is not listing try catch (for details of why see, for example, this related answer). And I assume, that implementing try catch on a µ-controller could be kind of difficult/impossible.

Try catch in most languages is a quite expensive operation: The program stack get copied once for the try block and for each catch block. In case the try goes wrong the try-block stack will be discarded and one of the catch block stacks will be executed.
I am not an expert of cpu architecture, but I can imagine, that this needs a lot of memory swapping and similar operations — it should be hard to achieve with a simple µ-controller.

The Arduino reference is not listing try catch (for details of why see, for example, this related answer). And I assume, that implementing try catch on a µ-controller could be kind of difficult/impossible.

Try catch in most languages is a quite expensive operation: The program stack get copied once for the try block and for each catch block. In case the try goes wrong the try-block stack will be discarded and one of the catch block stacks will be executed.
I am not an expert of cpu architecture, but I can imagine, that this needs a lot of memory swapping and similar operations — it should be hard to achieve with a simple µ-controller.

It might worth to look how C-Programmers do patterns similar to try/catch

Leave a Comment