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

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.*;
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.

1 Like

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

I believe we can check whether it had worked by calling method Client::active(): :electric_plug:
https://processing.org/reference/libraries/net/Client_active_.html

2 Likes

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

  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.

1 Like
  • Just a tip: Avoid directly checking for true or false. :warning:
  • B/c we can accidentally type in the assign = operator instead of the equality == 1, and Java won’t complain about it! :shushing_face:
  • For checking for false use the ! logical NOT operator: if (!c.active()) { :exclamation:
  • Processing.org/reference/logicalNOT.html
  • For checking for true, just do not use any operator: if (c.active()) { :blush:
1 Like

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

–BF

1 Like

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

:slight_smile: