Package org.kiwiproject.concurrent
Class AsyncException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.kiwiproject.concurrent.AsyncException
- All Implemented Interfaces:
Serializable
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:
- 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 declaringgetFuture()in a generic manner (and suppressing the unchecked warning). This means you could still receive aClassCastExceptionat runtime if you attempt a cast to an invalid type.
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncException(String message, @Nullable CompletableFuture<?> future) Construct instance with given message and future.AsyncException(String message, Throwable cause, @Nullable CompletableFuture<?> future) Construct instance with given message, cause, and future. -
Method Summary
Modifier and TypeMethodDescription<T> @Nullable CompletableFuture<T>The future which causes the exception.booleanDoes this AsyncException contain a future?Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
AsyncException
Construct instance with given message and future.- Parameters:
message- the exception messagefuture- theCompletableFuturethat caused the error, may be null
-
AsyncException
Construct instance with given message, cause, and future.- Parameters:
message- the exception messagecause- the original cause of the exceptionfuture- theCompletableFuturethat caused the error, may be null
-
-
Method Details
-
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
The future which causes the exception. May be null. UsehasFuture()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
-