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
public class AsyncException extends RuntimeException
A subclassRuntimeExceptionused to indicate problems in asynchronous code.The cause will generally be an
InterruptedExceptionor one of the checked exceptions thrown by Java's futures, specifically eitherTimeoutExceptionorExecutionException- 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 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
Constructors Constructor Description AsyncException(String message, Throwable cause, CompletableFuture<?> future)Construct instance with given message, cause, and future.AsyncException(String message, CompletableFuture<?> future)Construct instance with given message and future.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> CompletableFuture<T>getFuture()The future which causes the exception.booleanhasFuture()Does 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 Detail
-
AsyncException
public AsyncException(String message, @Nullable CompletableFuture<?> future)
Construct instance with given message and future.- Parameters:
message- the exception messagefuture- theCompletableFuturethat 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 messagecause- the original cause of the exceptionfuture- theCompletableFuturethat 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. 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
-
-