Reputation: 358
I have a program which uses executorService
to which I am passing callables
.
Each of which is an object of one class which implements java.util.concurrent.Callable
.
Then the executorService
is invoked. A java.lang.NoClassDefFoundError
is thrown in the middle of one of the callables in the call()
method.
However it is not terminating nor getting logged on the console. Therefore there is no way to know if the program has worked correctly or not. Kindly suggest any way by which I can understand the same.
Upvotes: 1
Views: 1733
Reputation: 358
Some one had already posted answer to this question, but when I came to mark it as answer after verifying, the post was unfortunately deleted. I am just retyping the answer.
The Futures which are returned by the ExecutorService after calling the invoke method contain all the Future objects of the thread. If anything goes wrong in any of the threads, an java.util.concurrent.ExecutionException
is thrown. Which can be detected in the parent thread which owns the executorService when we do get()
on the Future. Then e.getCause()
after catching it will get us the actual object which caused an error/exception.
Upvotes: 0
Reputation: 96394
In order to print the error to the console, you can create a Thread.UncaughtExceptionHander. Passing it into the Thread#setDefaultUncaughtThreadExceptionHandler will cause the handler to be invoked when the error is thrown.
Upvotes: 2
Reputation: 121
A Callable throws an Exception, which is not the superclass of NoClassDefFoundError. Within your Callable, catch Error (or even Throwable) and wrap it with an Exception:
V call() throws Exception
{
try
{
return this.doSomething();
} catch (Error e) {
e.printStackTrace();
throw new Exception(e);
}
}
Upvotes: 2