23

I am working with socket communication in Arduino, and I need the try/catch block for proper handling, what do you guys suggest? My search on the internet wasn't successful.

edit:

The code I am working with uses the WiFly module to interact with a mobile application, I am building a robot module with some controls over mobile application using Android. Everything works just fine, but sometimes the socket gets disconnected, so I need to add handling for such cases, something similar to try/catch block, but I didn't find similar block for Arduino.

My code:

void loop() {
    Client client = server.available();
    if (client) {
        while (client.connected()) {
            if (client.available()) {
                // Serial.print("client connected \n");
                char c = client.read();

                if(c == 'L')
                    turnLeft();
                if(c == 'R')
                    turnRight();
                if(c == 'F')
                    goForward();
                if(c == 'B')
                    goBackward();
                if(c == 'S')
                    Stop();

                Serial.print(c);
            }
        }

        // give the web browser time to receive the data
        delay(100);
        client.stop();
    }
}
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
NZal
  • 849
  • 3
  • 9
  • 19
  • You need to give us some more information...Like what you have tried, what you have find... – user123_456 Apr 19 '12 at 12:57
  • I have just updated the question – NZal Apr 19 '12 at 13:04
  • 1
    https://arduino.stackexchange.com/a/234 - alternative would be using a watchdog timer to watch for errors. And with some clever eeprom tracking changes, on restart you can figure out what next did not succeed, record it as skipped, and always skip it (or however you want to handle it). – TamusJRoyce Apr 15 '19 at 03:38
  • 1
    [This repo](https://github.com/meaning-matters/Except) contains a try/catch implementation for C if anyone really needs it. – mikeLundquist May 13 '22 at 11:19

3 Answers3

31

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

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
16

Arduino doesn't support exception handling. However, you don't need to use exception handling to make your code robust. By simply checking the return values of functions that can fail you can achieve the same end.

Since client.connected() is checked every time around the loop, and since client.available() will return 0 if not connected the only possible failure that is not already being handled is the return from client.read().

You can fix this, for example, by changing the line:

char c = client.read();

to:

int i = client.read();
if (i == -1) {
    break;
}
char c = (char) i;
Community
  • 1
  • 1
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
  • 1
    :( that error handling is not available... the mentioned alternative "robust return value checking" is doubtable. many functions like i.e Serial.print give little to no info whether they executed sucessfuly. It is an option but at present I am still stuck how to figure to avoid my skech from crashing by running out of ram because Serial.print is not getting away its data! – humanityANDpeace Mar 07 '13 at 08:09
  • Running out of SRAM is a problem I've encountered a number of times and you're right, there isn't any good way to check this programmatically. Often the most obvious symptom of this is corrupt Serial output. If you just want to get a sense for how your sketch is using memory you can check the amount of available SRAM using the MemoryFree library (the simplest way is to copy the MemoryFree.cpp code from http://playground.arduino.cc/Code/AvailableMemory into your sketch and call `freeMemory()`). – Matthew Murdoch Mar 07 '13 at 10:42
2
/* this builds successfully */
try {
  /* code */
} catch(String error) {

}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31