It's not a secret anymore!
The moose likes Threads and Synchronization and the fly likes Exceptions from Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Exceptions from Runnable" Watch "Exceptions from Runnable" New topic

Exceptions from Runnable

Dave Barnes

Joined: Jun 10, 2013
Posts: 5
Hi I am new to java so not sure if i am going the right way about this. I am using the executor services and my threads are implementing runnable.

What i want to be able to do is return results or any exceptions back to the calling class. I cant do this with runnable, so have been reading up on Runnable with a Future. ot sure how this would be implemented. In my caling class I can make checks to see if the thread is complete (isdone() ?) and get any exceptions with get(), but in the class which implements the runnable not sure how I would set any exceptions that can be picked up by the calling class? Also would the calling class have to sit in a continuous while loop waiting for the isdone() to complete?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
The javadocs of the Future class have an example of how to return a String object from thread, but you use any other class instead of String.

As for handling exceptions, you should catch ExecutionException - that's what the JVM will throw if the Future class threw an exception.
I agree. Here's the link:
subject: Exceptions from Runnable
It's not a secret anymore!