Where to catch Exceptions thrown from Callable.call()
AFAIR java.util.concurrent.Future.get()
will throw ExecutionException
if provided callable threw exception in the past (the exception is stored in the Future
).
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.
Related videos on Youtube
nano7
Updated on March 03, 2020Comments
-
nano7 about 4 years
Possible Duplicate:
Handling exceptions from Java ExecutorService tasksI use the
ExecutorService
from Java for coordinating Threads. For starting the threads I usepool = new ExecutorService(2); callableResults = pool.invokeAll(threads);
To collect the result, I use
future.get()
for each thread. "threads" is a List of Objects from a Class which implementsCallable
and overridescall()
.Now Ive got the following problem. The method
call()
does throw various specific exceptions.invokeAll()
andfuture.get()
throw onlyInterruptedException
.Where can I catch my specific exceptions which I throw in
call()
? Or do I have to handle them there? If one of those exceptions is thrown, is the result then aInterruptedException
?-
nano7 about 12 yearsI read this before, but I did not see that he uses Callable and call() as well? Maybe Ive overseen it!? edit: correct. Ive overseen it. sorry.
-
Mikko Maunu about 12 yearsIn this answer: stackoverflow.com/a/2248166/118846
-
Gray about 12 yearsHe uses Runnable but the exception catching is the same.
-
skaffman about 12 years
Future.get()
throwsInterruptedException
andExecutionException
. You need to read the javadocs more carefully. -
Brian Roach about 12 yearsThe answer (stackoverflow.com/a/2248166/302916) does.
-
nano7 about 12 yearsYes. Sorry. I only saw the InterruptedException.
-
-
Farid about 3 yearsThe very reason to use
ExecutorService
is its ability to run without blocking.get()
, on the other hand, is blocking until the task is executed. Seems like this is one of the major drawbacks ofExecutorService