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

**URL:** <https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524>\
**Category:** Coding Questions\
**Created:** [December 19, 2019, 6:07pm UTC](https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524 "2019-12-19T18:07:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pacolux](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pacolux/32/1893_2.png) [@pacolux](https://discourse.processing.org/u/pacolux)\
**Post date:** [December 19, 2019, 6:07pm UTC](https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524/1 "2019-12-19T18:07:18Z")

</div>

```auto
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](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

---

<div class="post-metadata">

**Author:** ![Sonarmiss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sonarmiss/32/7594_2.png) [@Sonarmiss](https://discourse.processing.org/u/Sonarmiss)\
**Post date:** [December 24, 2019, 1:58pm UTC](https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524/2 "2019-12-24T13:58:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 24, 2019, 2:27pm UTC](https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524/3 "2019-12-24T14:27:23Z")

</div>

> [@pacolux](#):
>
> catch (ConnectException e)  
> {println(“a bad thing happened”);}

Try changing this to

```auto
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 bloc_k 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 😀

---

<div class="post-metadata">

**Author:** ![pacolux](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pacolux/32/1893_2.png) [@pacolux](https://discourse.processing.org/u/pacolux)\
**Post date:** [December 30, 2019, 3:27pm UTC](https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524/4 "2019-12-30T15:27:44Z")

</div>

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…

```auto
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");}
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 30, 2019, 5:23pm UTC](https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524/5 "2019-12-30T17:23:32Z")

</div>

OK I ran the following code

```auto
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

```auto
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](https://www.google.com/search?client=firefox-b-d&q=java.net.SocketInputStream.read), including info about hang-ups.

---

<div class="post-metadata">

**Author:** ![pacolux](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pacolux/32/1893_2.png) [@pacolux](https://discourse.processing.org/u/pacolux)\
**Post date:** [January 20, 2020, 4:37am UTC](https://discourse.processing.org/t/try-catch-connectexception-e-seems-to-be-missing/16524/6 "2020-01-20T04:37:34Z")

</div>

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.
