Details
-
New Feature
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
None
-
None
Description
Exceptions of the checked type ExecutionException are thrown by the Java 1.5 task execution framework if the execution of a task fails. Dealing with these exceptions is somewhat cumbersome as they wrap all kinds of possible error conditions: checked exceptions, runtime exceptions, and errors as well. Therefore in Java Concurrency in Practice (JCIP) the following approach is suggested:
- First inspect the root cause of the exception and check whether it is one of the possible checked exceptions. If this is the case, re-throw the checked exception (after a type cast).
- After that call the utility method launderThrowable(). launderThrowable() deals with errors and runtime exceptions and throws them accordingly.
This approach ensures that all possible exception types are correctly extracted from an ExecutionException and passed to the caller.
This ticket is about adding support for this exception extraction process to Commons Lang.
With regards to checked exceptions, there is probably nothing we can do to simplify exception handling as Java does not allow throwing arbitrary checked exceptions from a method. However, we could at least add a method like launderThrowable() to an utility class (e.g. ConcurrentUtils, which could provide other functionality related to the concurrent package as well).
We could also go a step further and define a custom Exception class, say ConcurrentException, which plays a similar role as ExecutionException, but is guaranteed to wrap only a checked exception. An utility method converting ExecutionException to ConcurrentException would - like launderThrowable() - check for runtime exceptions and errors and handle them directly. All other exceptions are wrapped in a ConcurrentException. In cases where no checked exceptions are needed or desired, also a runtime version (ConcurrentRuntimeException) could be provided.