Exception handling


I come from C# and I'm used to work with try-catch blocks in order to deal with exceptions.

I've found out that in Arduino this is not used as it requieres lots of resources for a small uC, so, I wonder now what happens when there is an exception in code.

Now I'm using the serial port to communicate with other devices that I didn't program. So, I have some doubts related to how robust is my code.

Actually I have not a problem yet, but I would appreciate you tell me some best practices I should follow. Should I always enable watchdog? What happens if I try to convert a string wich it was suposed to contain numbers to int, but I receive not numbers but characters? Or what happens if I receive a too long string? Will the device run out of memory in some cases?

I'm sorry if it's a wide and not focused question, but I appreciate any suggestion.

Thanks in advance

The short answer is that you need to anticipate all the exceptions and include code to deal with them. For example your code could throw away extra characters if it receives too many.


Yes, validating human input is critical.
Programming / runtime errors aren’t allowed in embedded code.
You need to ensure integrity of values, or test for internally passed values when you’re testing your code.

I wonder now what happens when there is an exception in code.

There are no exceptions.
Anything that might go wrong, you need to check for manually. (some of this is just C/C++ (no exception for exceeding the bounds of an array), and some is the hardware involved (no "exception" instructions, nor instructions that generate exception-like events (math overflow, invalid memory access, etc.))