How to catch exception thrown by another thread in Java?

23,898

Solution 1

If you can not catch it maybe that helps you:

If you have the Thread object you can try to set a UncaughtExceptionHandler. Take a look at Thread.setUncaughtExceptionHandler(...).

Give us some more detail about the library you use and how you use it.

Solution 2

If all you have is a Thread object then there is no way to catch any exceptions (which I assume are RuntimeException). The proper way to do this is by using the Future<?> class used by the ExecutorService but you don't have control over the code starting the Thread I assume.

If you are providing the Runnable or if you are injecting any of the code into the library, then you could wrap it in a class that catches and holds the Exception for you but that is only if the exception is in your code or is thrown from within code you are calling. Something like the following:

final AtomicReference<Exception> exception = new AtomicReference<Exception>();
Thread thread = library.someMethod(new Runnable() {
   public void run() {
      try {
         // call a bunch of code that might throw
      } catch (Exception e) {
         // store our exception thrown by the inner thread
         exception.set(e);
      }
   }
});
// we assume the library starts the thread
// wait for the thread to finish somehow, maybe call library.join()
thread.join();
if (exception.get() != null) {
   throw exception.get();
}

Also, if you are forking your own thread you can also set the uncaught exception handler:

thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
   public void uncaughtException(Thread t, Throwable e) {
      // log it, dump it to the console, or ...
   }
});

But if the thread code inside of the library cannot be wrapped by you then that won't work. If you edit your question and show some code and provide some more details, I can edit my question to provide better help.

Share:
23,898
yuejdesigner85
Author by

yuejdesigner85

Updated on July 09, 2022

Comments

  • yuejdesigner85
    yuejdesigner85 almost 2 years

    I'm using a library that creates its own thread and it throws an exception. How can I catch that exception? The exception is thrown on the line marked below:

    ResourceDescriptor rd = new ResourceDescriptor();
            rd.setWsType(ResourceDescriptor.TYPE_FOLDER);
            fullUri += "/" + token;
            System.out.println(fullUri);
            // >>> EXCEPTION THROWN ON THE FOLLOWING LINE <<<
            rd.setUriString(fullUri.replaceAll("_", ""));
            try{
                rd = server.getWSClient().get(rd, null);
            }catch(Exception e){
                if(e.getMessage().contains("resource was not found")){
                    this.addFolder(fullUri, label, false);
                    System.out.println("Folder does not exist, will be added now.");
                }else{
                    System.out.println("Error Messages: " + e.getMessage());
                }
            }