# Capturing Net Client Error if Server Not Running

**URL:** <https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536>\
**Category:** Libraries\
**Created:** [February 4, 2020, 1:53am UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536 "2020-02-04T01:53:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![redeployed](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@redeployed](https://discourse.processing.org/u/redeployed)\
**Post date:** [February 4, 2020, 1:53am UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536/1 "2020-02-04T01:53:58Z")

</div>

Hi, I’m trying to program a client application based on the SharedCanvasClient example. My current task is to handle the case where the server might not be running. When the server example is not running and I run the client example, it responds with:

> java.net.ConnectException: Connection refused: connect

So, I tried the try/catch commands:

```auto
  try{
  c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
  } catch (java.net.ConnectException e){
    println("caught it");
  }

```

it responds with:

> Unreachable catch block for ConnectException. This exception is never thrown from the try statement body

My goal is to retry, or put a status message up on the screen of the client program if the server can not be reached.

Can someone point me in the right direction to address this?

thank you

---

<div class="post-metadata">

**Author:** ![anon68884268](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon68884268](https://discourse.processing.org/u/anon68884268)\
**Post date:** [February 4, 2020, 6:00pm UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536/2 "2020-02-04T18:00:11Z")

</div>

That’s more a workaround, but it happened to me and i used the simple class `Exception` for it.

---

<div class="post-metadata">

**Author:** ![redeployed](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@redeployed](https://discourse.processing.org/u/redeployed)\
**Post date:** [February 4, 2020, 6:22pm UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536/3 "2020-02-04T18:22:42Z")

</div>

Hi, thank you for the reply!

did you mean this:?

```auto
  try{
  c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
  } catch (Exception e){
    println("caught it");
  }

```

this gives me the original error I reported.

Or did you mean something else? I’m new to processing/java/etc. so I could be missing something obvious

---

<div class="post-metadata">

**Author:** ![anon68884268](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon68884268](https://discourse.processing.org/u/anon68884268)\
**Post date:** [February 6, 2020, 12:36pm UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536/4 "2020-02-06T12:36:52Z")

</div>

I think the problem is that the Client Constructor starts a new thread, but I have no idea, how to catch exceptions in a thread. Processing tries to follow the exception in your code, and if threads point outside of your sketchfolder, just your “thread caller” ist highlighted.  
Python for example does not matter if it is in **your** code, and a looong exception is shown to the mattering script.

---

<div class="post-metadata">

**Author:** ![redeployed](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@redeployed](https://discourse.processing.org/u/redeployed)\
**Post date:** [February 6, 2020, 2:37pm UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536/5 "2020-02-06T14:37:57Z")

</div>

Thanks again for your input! It sort of makes sense to me…  
I’ll do some more research and if I find a solution I will post.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 7, 2020, 12:56am UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536/6 "2020-02-07T00:56:17Z")

</div>

Hello,

Try this:

> **Modified SharedCanvasClient**
>
> ```auto
> /**
> * Shared Drawing Canvas (Client) 
> * by Alexander R. Galloway. 
> * 
> * The Processing Client class is instantiated by specifying a remote 
> * address and port number to which the socket connection should be made. 
> * Once the connection is made, the client may read (or write) data to the server.
> * Before running this program, start the Shared Drawing Canvas (Server) program.
> */
> 
> import processing.net.*;
> 
> Client c;
> String input;
> int data[];
> 
> boolean clientConnected = false;
> 
> void setup() 
> {
> size(450, 255);
> background(204);
> stroke(0);
> frameRate(5); // Slow it down a little
> // Connect to the server's IP address and port
> while(!clientConnected)
> {
> c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
> if(!c.active())
> {
> println("Not connected to server");
> println("Start the server");
> clientConnected = false;
>       
> }
> else
> {
> println("Connected to server");
> clientConnected = true;
> }
> }
> }
> 
> void draw() 
> {
> if (clientConnected)
> {
> // Do something  
> }
> else
> {
> // Do something else
> }
> }
> 
> ```

See this topic:

> [@Exception handling in processing.net.\* -- why can't I catch exceptions?](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192):
>
> ME: Computer science professor. Reasonably adept at processing, though not a guru. GOAL: To handle the exception in the code below, because I figure this is something my students will run into: import [processing.net](http://processing.net).\*; Client myClient; void setup() { size(400, 400); try { myClient = new Client(this, “192.168.2.2”, 5025); } catch (Exception e) { println(“Looks like server isn’t running”); } } When I run the code above without a server for it to connect to, the exception is not cau…

’ 🙂 ’

---

<div class="post-metadata">

**Author:** ![redeployed](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@redeployed](https://discourse.processing.org/u/redeployed)\
**Post date:** [February 7, 2020, 1:25am UTC](https://discourse.processing.org/t/capturing-net-client-error-if-server-not-running/17536/7 "2020-02-07T01:25:29Z")

</div>

Hi glv!  
Thank you so much for the solution to my problem!

Kind of embarrassing, I looked through the net library reference and did not notice active().

cheers,
