Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does FutureTask lack something like exceptionOccurred()  RSS feed

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really like the FutureTask since java 5.0, but what I miss is a method like:


With FutureTask.exceptionOccurred() it would nicely fit to the other future queries like isDone() or isCancelled() and is much better readable as this try/catch construct.

Maybe there is a certain reason why such a exception-query method is missing?

ooops: could moderator please move this to java threads and synchronization.
 
Henry Wong
author
Sheriff
Posts: 22827
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hmmmmm..... It does seem to be missing the method, doesn't it?

Well, you can always write your own implementation of the Future class. One does take another Future instance, and passes all requests to it. This can be enhanced with an isException() and getException() method that does all the work -- and hide all the try/catch stuff from you.

Henry
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this is more of a rhetorical question that really should be directed at the writers of the concurrency package. But I think they chose to stay with the tradition Java Exception mindset. Exceptions are not normal behavior, they require you to either fix the problem or report the problem, and so they require a forced, specific code flow construct for handling. Since the code you are using to get() results from the FutureTask may have had an exception, you would need to be able to handle the possible exceptions in the thread calling get(). Passively letting exceptions pass by not forcibly reporting the exception to the get()ing thread could lead to non-working code. In order to create safe code you would have to remember to check things like:


I don't see the benefit over the try/catch code:


What I like about the try/catch code is that
1) The code for working with the successful get() is kept close to the get() call
2) The exceptions are explicit (interruption versus execution error), and the code for handling the two scenarios is kept independent
3) I can't forget to handle the exception, and thus try to process results from a task which failed execution.

On the other hand, If you really wanted to I am sure you could write a wrapper class which takes the exception handling away and provides the methods you want. Perhaps something like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!