• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question in Callable interface

 
Md Fizal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class called "Worker" which implements java.util.concurrent.Callable interface. When I create a Worker object and submit to an ExecutorService, I get a Future object back. My question is, if WorkerThread throws an exception, how do I catch it?

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that's a weird situation. Try throwing an exception and see if you get a stack trace in the log. You could catch it anywhere in the trace ... if any of the classes in the trace are yours to modify.
 
Henry Wong
author
Sheriff
Posts: 22516
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Md Fizal:
I have a class called "Worker" which implements java.util.concurrent.Callable interface. When I create a Worker object and submit to an ExecutorService, I get a Future object back. My question is, if WorkerThread throws an exception, how do I catch it?


It has been awhile since I worked with Java 1.5 -- too many clients still using Java 1.4... but... if I remember correctly...

If you try to get() the result from the future object, it will throw an execute exception, if the callable task threw an exception. You simply have to catch the execute exception. To get the original exception that was thrown, you simply call the getCause() method of the execution exception.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty neat. I didn't read down to the get() method doc to see the exceptions before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!