# Try catch with serial

**URL:** <https://discourse.processing.org/t/try-catch-with-serial/1148>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [June 21, 2018, 10:26am UTC](https://discourse.processing.org/t/try-catch-with-serial/1148 "2018-06-21T10:26:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [June 21, 2018, 10:26am UTC](https://discourse.processing.org/t/try-catch-with-serial/1148/1 "2018-06-21T10:26:21Z")

</div>

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 :

```auto
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 🙂

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

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 22, 2018, 9:06am UTC](https://discourse.processing.org/t/try-catch-with-serial/1148/2 "2018-06-22T09:06:06Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [June 22, 2018, 12:57pm UTC](https://discourse.processing.org/t/try-catch-with-serial/1148/3 "2018-06-22T12:57:01Z")

</div>

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)  
…

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [June 22, 2018, 5:00pm UTC](https://discourse.processing.org/t/try-catch-with-serial/1148/4 "2018-06-22T17:00:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 24, 2018, 10:18pm UTC](https://discourse.processing.org/t/try-catch-with-serial/1148/5 "2018-06-24T22:18:15Z")

</div>

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

```java
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);  
}

```

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [June 25, 2018, 9:49am UTC](https://discourse.processing.org/t/try-catch-with-serial/1148/6 "2018-06-25T09:49:04Z")

</div>

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();  
}
