# Exception handling in processing.net.\* -- why can't I catch exceptions?

**URL:** <https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192>\
**Category:** Libraries\
**Created:** [June 19, 2019, 7:09pm UTC](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192 "2019-06-19T19:09:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![barryfagin](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@barryfagin](https://discourse.processing.org/u/barryfagin)\
**Post date:** [June 19, 2019, 7:09pm UTC](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192/1 "2019-06-19T19:09:09Z")

</div>

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 caught. Instead, a java.net.ConnectException is raised with various diagnostic info in the console window. I have some suspicion this relates to Java and threads, but I bet there are people here who know more. Bottom line: Is this problem fixable within Processing? If not, I suspect the benefits are not worth the costs for my smart but beginner students, so I won’t bother. Still curious though as to what exactly is going on and what the fix might be.

Thanks to all!

Barry F.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 19, 2019, 8:57pm UTC](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192/2 "2019-06-19T20:57:02Z")

</div>

Most of Processing’s API, and even some Processing’s 3rd-party libraries, prefer to swallow exceptions rather than throw them. ⚾

I believe we can check whether it had worked by calling method Client::**active()**: 🔌  
[https://processing.org/reference/libraries/net/Client\_active\_.html](https://processing.org/reference/libraries/net/Client_active_.html)

---

<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:** [June 20, 2019, 1:49am UTC](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192/3 "2019-06-20T01:49:59Z")

</div>

I have been using the SharedCanvasClient and SharedCavasServer to test this.

```auto
  if (c.active() == false) 
    {
    println("Client not Connected");
    clientFlag = false;
    }

```

Processing still displays all the “gobbledygook” about what happened if you can’t connect to server but you can still proceed and use the clientFlag as you wish; I used it so I would not use any of the client methods for now. I may try reconnecting to server in future exploration of this.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 20, 2019, 5:56am UTC](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192/4 "2019-06-20T05:56:04Z")

</div>

> [@glv](#):
>
> `if (c.active() == false) {`

- Just a tip: Avoid directly checking for `true` or `false`. ⚠
- B/c we can accidentally type in the [assign `=`](https://processing.org/reference/assign.html) operator instead of the [equality `==`](https://processing.org/reference/equality.html) 1, and Java won’t complain about it! 🤫
- For checking for `false` use the `!` logical NOT operator: `if (!c.active()) {` ❗
- [! (logical NOT) / Reference / Processing.org](http://Processing.org/reference/logicalNOT.html)
- For checking for `true`, just do not use any operator: `if (c.active()) {` 😊

---

<div class="post-metadata">

**Author:** ![barryfagin](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@barryfagin](https://discourse.processing.org/u/barryfagin)\
**Post date:** [June 27, 2019, 6:26pm UTC](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192/5 "2019-06-27T18:26:04Z")

</div>

Thank you all! c.active() it is.

–BF

---

<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:** [July 3, 2019, 12:17am UTC](https://discourse.processing.org/t/exception-handling-in-processing-net-why-cant-i-catch-exceptions/12192/6 "2019-07-03T00:17:04Z")

</div>

I posted an example of some recent work in the gallery:

> [@Networking: Processing Client to PHP Server](https://discourse.processing.org/t/networking-processing-client-to-php-server/12466):
>
> A very simple example of a Processing network client communicating with a PHP server. I used the WAMP server: An “update” increments a counter (Processing) and is sent to a server( with c.write()) and stored in a file on server. You can run the web page (reads “counter” from file) to watch the updates or just open the file to see current status. Restarting the Processing sketch resets the counter on Processing and the saved counter on server. “Updates” are sent with a mouse left button …

🙂
