Search code examples
exceptionservertry-catchprocessing

Processing doesn't catches exceptions


try
{
  println("Connecting to the server....");
  myClient = new Client(this, "192.168.0.185", 1111);
  serverworks = true;
  println("Connected to the server!");
  println(myClient);
}
catch(Exception e)
{
  print("Exception: ");
  println(e);
}

In this case, it throws

Connecting to the server....
java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:579)
    at java.base/sun.nio.ch.Net.connect(Net.java:568)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at java.base/java.net.Socket.connect(Socket.java:583)
    at java.base/java.net.Socket.<init>(Socket.java:507)
    at java.base/java.net.Socket.<init>(Socket.java:287)
    at processing.net.Client.<init>(Unknown Source)
    at G7D3T.checkbuttons(G7D3T.java:178)
    at G7D3T.draw(G7D3T.java:100)
    at processing.core.PApplet.handleDraw(PApplet.java:2185)
    at processing.awt.PSurfaceAWT$9.callDraw(PSurfaceAWT.java:1440)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:356)
Connected to the server!
processing.net.Client@2a3942d7

But if I want to catch that specific exception (java.net.ConnectException), the code doesn't even compiles:

catch(java.net.ConnectException e)

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


Solution

  • In the first case you are just seeing a message but the exception is not thrown, otherwise you would not see the output "Connected to the server!". Maybe your Client class is not throwing the exception but just printing it.