Try...Catch

Hello. I run this code, but an error comes up.

try { 
  port.write(222);
} 
catch (Exception e) {
  offScreen = 1;
}

Error: expected type-specifier before 'Exception'

Any ideas? Thanks

That's probably because that is (or looks like) Java, and the Arduno is programmed in C++.

I run this code,

Where?

This is an sample on processing.

When I try to compile this code on arduino.app this error appears.

Is there a way to use this code in arduino language? :/

Thanks so much for your replies

If the problem is with a piece of processing code, then I suggest you ask on the processing forums: http://forum.processing.org/

If you want to get an answer on here, or anywhere, you are going to have to provide more than just that little snippet. It is guaranteed that the error is caused by an error somewhere else in your program. If you post that same snippet on the processing forums with the same question they will immediately tell you to post your whole code, or at least a SSCCE.

No. My question is how to make this code to work on arduino language.

This is the code.

Oh right. Well, in that case the answer is simple:

You can't. The whole concept is meaningless. There is no exception system, so you cannot try and catch. Anyway, there would be nothing to catch. You write character 222 to the serial. How can that "fail"?

What are you trying to do?

Processing is Java. "Arduino language" is C++ - what exceptions do you expect on a microcontroller? (In C++ it's try...throw / catch)

If the connection has been lost. :)

Anyway, Thanks for much for the answers!!:) I was afraid it would be something like that

Thanks again, dude!

void setup ()
  {
  }  // end of setup

void loop ()
  {
  try
    {
      
    }
  catch (...)
    {
    
    }
  }  // end of loop

That code is OK, however if you enable verbose compiling:

/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions

The IDE disables exceptions, giving this error message:

sketch_aug26a.ino: In function 'void loop()':
sketch_aug26a:11: error: exception handling disabled, use -fexceptions to enable

If the connection has been lost.

Serial.print doesn’t throw exceptions, so your try/catch would not catch them. Since exceptions are disabled none of the core libraries throw exceptions.

There is no such concept as a connection with Serial. It's like throwing a ball. You can throw the ball at someone. Whether they can catch it or not is not your problem. You can even thrown the ball when they're not there. You're just throwing a ball. In the OSI model Serial would be layer 2 - the Data layer. It is just a method of transmitting data along a wire. The concept of a connection doesn't come into play until at least layer 5 - the Session (I was taught "socket", not "session") layer. Maybe not even then - maybe there is no concept of the connection until layer 6 - the Presentation (again, I was taught this was "Protocol", but I guess that's a difference between UK and USA) layer.

(FYI: OSI 7 layer model Wikipedia page)

So if you try to throw the ball it might not throw an exception, and thus if the ball isn't caught the exception isn't caught?

while ( !( Understanding = Try...Catch is something that engages an Operating System )) { Post question about using Try...Catch on Arduino }