0

I'm developing a Swing application that also does use a REST-API. I'm using Jersey to accomplish this.

I want the user to input a URL into a TextField and would like to give him some feedback if the URL is malformed or anything. I'm using URI as a basis to store the URL and am able to catch any problems that arise with that and give the user some informations.

 } catch (URISyntaxException ex) {
        MainClass.write2Log("URL wrong : '" + baseURL + "'");
        }

Now, when the URL has the right format, but simply doesn't resolve, I get this exception :

Exception in thread "AWT-EventQueue-0" javax.ws.rs.ProcessingException:
java.net.UnknownHostException: sx.dddsds.ded
at org.glassfish.jersey.client.HttpUrlConnector.
apply(HttpUrlConnector.java:205)

So my questions is this : How can I 'catch' this exception when I don't have the Object that throws this exception?

Thanks in advance !

cete3
  • 647
  • 6
  • 19
  • possible duplicate of [How to catch an Exception from a thread](http://stackoverflow.com/questions/6546193/how-to-catch-an-exception-from-a-thread) – sschuberth Jan 22 '14 at 09:26

2 Answers2

2

Exceptions travel up the call stack on the thread they were thrown in; they don't cross threads. If you want to catch an exception, you need your try-catch block somewhere in the stack trace that you didn't post in your question.

Looking at the lines you did post, it's looking like you're trying to execute a long-running operation (an HTTP request) on the event thread; it should be running somewhere else.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • The exception occurs in `org.glassfish.jersey.client` which is a dependency, so I don't want to change any code in there. – cete3 Sep 24 '13 at 07:20
2

You can set it for particullar (for example current, or any other Thread instance) thread or globally for all uncaught exceptions:

Thread.setDefaultUncaughtExceptionHandler( ... );
Thread.currentThread().setUncaughtExceptionHandler( ...);

Starting a new thread by Thread you can set exception handler, and if you start new thread with executor you can set your custom thread factory, that will set exception handler.

Piotr Müller
  • 5,323
  • 5
  • 55
  • 82