Try catch with serial

Hi, I’m using an arduino that I plugged after my program is running.
I would like, if there is a problem that my app doesn’t crash.
I think to use this :

try {
    Serial serial = new Serial(this, "/dev/ttyACM0", 19200);
    receiver = new ValueReceiver(this, serial);
  }
  catch (IOException e) {
    e.printStackTrace();
  }

But i’ve got this error :slight_smile:

Unreachable catch block for IOException. This exception is never throw from the try statement body.

1 Like

The try{} block only works if the function or operation is designed to throw the error in the first place. For instance, if you are performing a division, result=x/y, you need to check that y is not zero or you throw an error that has to be handle by anybody using your function via try-catch blocks:

if(y==0){
  throw new ArithmeticException("You can\'t divide by zero!");
}

Now, you are trying to handle the error due to a serial connection or is it related to your ValueReceiver class?

Kf

1 Like

Run without the try and catch, look what error it throws and then put that error in the catch brackets.

catch()

Put it there with Exception type and variable name.
Examples:

catch(IOException e)
catch(NullPointerException e)

I’ve got this error : Error opening serial port /dev/ttyACM0; Port not found

If I try running your code (yes, accessing a linux port from a windows machine) I get “port is busy” type of error in run time. Not sure how I would capture that error. You will need to do some research on JavaDocs and the stackOverflow. Since I am in Windows, I know what ports are available for my serial to open. First check would be to, before opening the port, to check if the port is in the serial list. If it is not, then show an error. Notice that even if I try to open a port in the serial list that does not correspond to my arduino unit (I chose COM4 which I believe it is my B2), the following code opens it with no problem. I might be able to wrtie to it (broadcast on B2?) but this is not the point of this discussion. This operation is legit as it will open and occupy the port which it is not the arduino port as we know. So that is another type of error that you need to consider in your design. I am guessing you are creating a routine for any user to access your arduino device without knowing much about what is going on under the hood.

Kf

import processing.serial.*;

Serial ser;

//===========================================================================
// PROCESSING DEFAULT FUNCTIONS:

void settings(){
  size(400,600);
}

void setup(){

  textAlign(CENTER,CENTER);
  rectMode(CENTER);
  
  fill(255);
  strokeWeight(2);
  
  println(Serial.list());
  ser = new Serial(this, "COM4", 19200);  //COM4 is the only port listed. It is probably the bluetooth
  
  if(ser==null){println("DONE");}  //It didn't execute
}

void draw(){
  background(0);  
}

I just do this and now it’s ok. Thanks

try {
Serial serial = new Serial(this, “/dev/ttyACM0”, 19200);
receiver = new ValueReceiver(this, serial);
}
catch (Exception e) {
e.printStackTrace();
}