Try...catch (ConnectException e) seems to be missing

import java.net.*;

XML xml;

void setup() {
  try{
  xml = loadXML("http://192.168.24.50/tmp/latestsampledata_u.xml");
    }
      catch (ConnectException e)
        {println("a bad thing happened");}
}

Using this above to try to catch when the program cannot reach the URL to the XML file. It does not catch the exception. I just get the typical ConnectException error message and it does not run the code in catch.

“java.net.ConnectException: Connection timed out: connect
(…)
The file “http://192.168.24.50/tmp/latestsampledata_u.xml” is missing or inaccessible…”

Interestingly enough, I tried specifying “catch(ConnectException e)” and processing says the

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

Anyone have ideas?

K

1 Like

Sorry, but I didn’t really get what your question was…

Try changing this to

catch (Exception e)
{
  println(“a bad thing happened”);
}

The try - catch statement means
try some code and if an exception is thrown find a matching catch block to process the exception.

When Java experiences an error it sometimes throws an exception there are loads of different exceptions. In your example the code to try is

xml = loadXML(“http://192.168.24.50/tmp/latestsampledata_u.xml 2”);

loadXML is a method in PApplet and will throw a RuntimeException if it hits a problem. Since your catch block is only executed when a ConnectException is thrown it will never be executed hence the unreachable code message.

In my code it will catch any exception :grinning:

1 Like

Thanks for the response…

I had tried that. It will not run the catch code. I realized that the xml method already is in a ‘try…catch’ so I think that it is already catching the connection issue so there is nothing to catch. Currently I just make the assumption that if it comes back null it had a connection problem. Just trying to be a little more graceful. When I try (Exception e) it just times out and produces “java.net.ConnectException: Connection timed out: connect … BLAH BLAH” and does not print “a bad thing happened”.

this is the exact code ran…

import java.net.*;

XML xml;

void setup() {
try{
xml = loadXML("http://192.168.24.50/tmp/latestsampledata_u.xml");
}
catch (Exception e)
{println("a bad thing happened");}
}
1 Like

OK I ran the following code

XML xml;

void setup() {
  background(0);
  try {
    println("Start loading XML");
    xml = loadXML("http://192.168.24.50/tmp/latestsampledata_u.xml");
  }
  catch (Exception e) {
    println("Exception during loadXML method");
  }
  println("Finished loading");
}

void draw() {
  if (frameCount % 300 == 0) {
    background(0);
  }
  set((int)random(width), (int)random(height), color(255));
}

and then waited and thena waited some more and eventually I got the following exception message

Start loading XML
java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(SocketInputStream.java:210)
	at java.net.SocketInputStream.read(SocketInputStream.java:141)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
	at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:706)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
	at processing.core.PApplet.createInputRaw(PApplet.java:7145)
	at processing.core.PApplet.createInput(PApplet.java:7098)
	at processing.core.PApplet.createReader(PApplet.java:6904)
	at processing.core.PApplet.loadXML(PApplet.java:5995)
	at processing.core.PApplet.loadXML(PApplet.java:5985)
	at sketch_191230b.setup(sketch_191230b.java:23)
	at processing.core.PApplet.handleDraw(PApplet.java:2425)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Finished loading
The file "http://192.168.24.50/tmp/latestsampledata_u.xml" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

You can see that the exception was thrown when attempting to read the data in method
java.net.SocketInputStream.read(SocketInputStream.java:210)

Searching Google for this method produced some interesting results, including info about hang-ups.

2 Likes

I conceded to just make the assumption that XML returned NULL was an indicator that the connection did not occur. I got’s to move on.

Thanks so much for the help. Ill come back later and do some more research into getting this to work better.