Class AsyncException

  • All Implemented Interfaces:
    Serializable

    public class AsyncException
    extends RuntimeException
    A subclass RuntimeException used to indicate problems in asynchronous code.

    The cause will generally be an InterruptedException or one of the checked exceptions thrown by Java's futures, specifically either TimeoutException or ExecutionException

    See Also:
    Serialized Form
    Implementation Note:
    Sadly, we cannot make this class generic, i.e. AsyncException<T>. It will not compile; the compiler reports the following error: "a generic class may not extend java.lang.Throwable". However, we can fake it by declaring getFuture() in a generic manner (and suppressing the unchecked warning). This means you could still receive a ClassCastException at runtime if you attempt a cast to an invalid type.
    • Constructor Detail

      • AsyncException

        public AsyncException​(String message,
                              @Nullable
                              CompletableFuture<?> future)
        Construct instance with given message and future.
        Parameters:
        message - the exception message
        future - the CompletableFuture that caused the error, may be null
      • AsyncException

        public AsyncException​(String message,
                              Throwable cause,
                              @Nullable
                              CompletableFuture<?> future)
        Construct instance with given message, cause, and future.
        Parameters:
        message - the exception message
        cause - the original cause of the exception
        future - the CompletableFuture that caused the error, may be null
    • Method Detail

      • hasFuture

        public boolean hasFuture()
        Does this AsyncException contain a future?
        Returns:
        true if this instance contains a CompletableFuture
        API Note:
        When a single asynchronous operation is performed and there is only one future, then callers can expect this to contain a CompletableFuture and return true. When multiple futures are acted upon (e.g. waiting for all to complete), callers should expect this instance not to contain a CompletableFuture and this method to return false.
      • getFuture

        @Nullable
        public <T> CompletableFuture<T> getFuture()
        The future which causes the exception. May be null. Use hasFuture() to check if this instance contains a future.
        Type Parameters:
        T - the generic type of the CompletableFuture
        Returns:
        the future causing this exception, or null
        Throws:
        ClassCastException - if the type you assign you is not the actual type