2

I need to throw an exception. The only issue is that the exception is thrown from a thread started from within the method.

Here is an example of what I mean:

public void connectToWebsite(String URL){
    new Thread(){
        @Override
        public void run(){
           openURLConnection(URL);//Throws Malformed URL and IO Exception
        }
   };
}

This is part of an API so I want to allow the users of the API to handle the exception how they so choose. However, I cannot get connectToWebsite(url) to throw the exception as the exception is thrown within run. I cannot get run() to throw the exception as then it would not be overriding the run method. Is there a way I can get the exception is thrown without building an entire new thread class? Or should I just settle on adding a catch with printStackTraces?

I would ideally like a connectToWebsite to throw an exception and have the user handle it. However, is that even possible? If so, how? If I cannot or should not throw an Exception how should I notify the user something went wrong?

Skylion
  • 2,696
  • 26
  • 50
  • Which API you write to declare to throw exception, use it on methods –  Mar 02 '14 at 20:13
  • 1
    Did you see this? http://stackoverflow.com/questions/4491606/java-thread-run-method-cannot-throw-checked-exception – Martin Wilson Mar 02 '14 at 20:15
  • 1
    http://book.javanb.com/java-threads-3rd/jthreads3-CHP-13-SECT-5.html just found that. Take a look at it. Uncaught exceptions are handled via a method that can be rewritten. The thread in which the exception occured is passed as a parameter to this handler. –  Mar 02 '14 at 20:15
  • As @Thüzhen suggests, there is a default exception handler that "catches" exception that make it all the way to the root frame of a thread without being handled. The app developer can (if not prohibited by JVM authority rules) change this handler to do the developer's bidding. – Hot Licks Mar 02 '14 at 20:20

2 Answers2

6

Is there a way I can get the exception is thrown without building an entire new thread class? Or should I just settle on adding a catch with printStackTraces?

There are a couple of things you can do. The best is arguably to switch to use an ExecutorService and use a Callable instead of a runnable. You can submit your Callable to the ExecutorService which returns a Future. When you call future.get() it will throw an exception if your call() method threw an exception. It actually throws ExecutionException and the cause of that exception will be the exception thrown by call().

You can also use an UncaughtExceptionHandler and set it on the Thread. Then if you thread throws, the handler will be called with the exception.

Gray
  • 115,027
  • 24
  • 293
  • 354
0

Runnable cannot throw exception or return values.

One thing which could be done is to let the object which is created by openURLConnection to null. So the users will check

if (object == null) { /* something went wrong with url connection */ }
else {
/* all ok */
}

and knows if something went wrong or not.

It depends to how your API works.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53