Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to propagate exception from thread in java?

Code:

    outerMethod {
        @Override
        public void run() {
                innerMethod throws IOException
                }
    }

Method that exceuted in thread throws checked exception - IOException. I need to handle this exception in main thread. Like:

outerMethod() throws IOException
   {
        @Override
        public void run() {
                innerMethod() throws IOException
                }
    }

Is this possible? If no, what would be a better way to do this?

Thanks.

like image 700
user710818 Avatar asked Dec 21 '22 19:12

user710818


1 Answers

Use FutureTask http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/FutureTask.html#get%28%29 . It's get methods will encapsulate any exceptions from the task that might have run on another thread.

ExecutionException: Exception thrown when attempting to retrieve the result of a task that aborted by throwing an exception. This exception can be inspected using the Throwable.getCause() method.

like image 67
Gergely Szilagyi Avatar answered Dec 24 '22 03:12

Gergely Szilagyi