Class ExecutionExceptions

java.lang.Object
com.aoapps.lang.concurrent.ExecutionExceptions

public final class ExecutionExceptions extends Object
Utilities for working with ExecutionException.
  • Method Details

    • wrapAndThrow

      public static <Ex extends Throwable> void wrapAndThrow(ExecutionException ee, Class<? extends Ex> exClass, BiFunction<? super String,? super ExecutionException,? extends Ex> exSupplier) throws Ex
      Wraps and throws an ExecutionException when its cause is an instance of exClass. This is compatible with Lambda method references on common throwable constructors that take (String message, Throwable cause).

      First, an attempt is made to create a surrogate of the cause via Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable), with the execution exception being the cause of the new surrogate. When a surrogate cannot be created, uses the provided function exSupplier to create a new wrapper.

      When an ExecutionException occurs, unwrapping the cause may lose important stack trace information, since the cause is likely processed on a different thread and will not have the full caller stack trace.

      Furthermore, it is desirable to be able to maintain expected exception types. This wrapping will help maintain exception types while not losing critical stack trace information.

      This is expected to typically used within a catch block, to maintain exception types:

      try {
         …
         return future.get();
       } catch (ExecutionException ee) {
         wrapAndThrow(ee, IOException.class, IOException::new);
         throw ee;
       }

      When the cause is an InterruptedException and is wrapped via exSupplier, and the resulting surrogate is not itself an InterruptedException, the current thread will be re-interrupted.

      Parameters:
      exClass - Exceptions with causes of this class are wrapped and thrown.
      exSupplier - Performs wrapping of the execution exception itself when a surrogate cannot be created.
      Throws:
      Ex - When cause is an instance of exClass, throws ee wrapped via exSupplier.
      See Also:
    • wrapAndThrowWithTemplate

      public static <Ex extends Throwable> void wrapAndThrowWithTemplate(ExecutionException ee, Class<? extends Ex> exClass, BiFunction<? super Ex,? super ExecutionException,? extends Ex> exSupplier) throws Ex
      Wraps and throws an ExecutionException when its cause is an instance of exClass.

      First, an attempt is made to create a surrogate of the cause via Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable), with the execution exception being the cause of the new surrogate. When a surrogate cannot be created, uses the provided function exSupplier to create a new wrapper.

      When an ExecutionException occurs, unwrapping the cause may lose important stack trace information, since the cause is likely processed on a different thread and will not have the full caller stack trace.

      Furthermore, it is desirable to be able to maintain expected exception types. This wrapping will help maintain exception types while not losing critical stack trace information.

      This is expected to typically used within a catch block, to maintain exception types:

      try {
         …
         return future.get();
       } catch (ExecutionException ee) {
         wrapAndThrowWithCause(ee, SQLException.class, (template, cause)
           -> new SQLException(template.getMessage(), template.getSQLState(), template.getErrorCode(), cause)));
         throw ee;
       }

      When the cause is an InterruptedException and is wrapped via exSupplier, and the resulting surrogate is not itself an InterruptedException, the current thread will be re-interrupted.

      Parameters:
      exClass - Exceptions with causes of this class are wrapped and thrown.
      exSupplier - Performs wrapping of the execution exception itself when a surrogate cannot be created.
      Throws:
      Ex - When cause is an instance of exClass, throws ee wrapped via exSupplier.
      See Also: