- java.lang.Object
-
- com.aoapps.lang.concurrent.ExecutionExceptions
-
public final class ExecutionExceptions extends Object
Utilities for working withExecutionException.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <X extends Throwable>
voidwrapAndThrow(ExecutionException ee, Class<? extends X> xClass, BiFunction<? super String,? super ExecutionException,? extends X> xSupplier)static <X extends Throwable>
voidwrapAndThrowWithTemplate(ExecutionException ee, Class<? extends X> xClass, BiFunction<? super X,? super ExecutionException,? extends X> xSupplier)
-
-
-
Method Detail
-
wrapAndThrow
public static <X extends Throwable> void wrapAndThrow(ExecutionException ee, Class<? extends X> xClass, BiFunction<? super String,? super ExecutionException,? extends X> xSupplier) throws X extends Throwable
Wraps and throws anExecutionExceptionwhen its cause is an instance ofxClass. 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 functionxSupplierto create a new wrapper.When an
ExecutionExceptionoccurs, 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; }- Parameters:
xClass- Exceptions with causes of this class are wrapped and thrown.xSupplier- Performs wrapping of the execution exception itself when a surrogate cannot be created.- Throws:
X- When cause is an instance ofxClass, throwseewrapped viaxSupplier.X extends Throwable- See Also:
Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable)
-
wrapAndThrowWithTemplate
public static <X extends Throwable> void wrapAndThrowWithTemplate(ExecutionException ee, Class<? extends X> xClass, BiFunction<? super X,? super ExecutionException,? extends X> xSupplier) throws X extends Throwable
Wraps and throws anExecutionExceptionwhen its cause is an instance ofxClass.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 functionxSupplierto create a new wrapper.When an
ExecutionExceptionoccurs, 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; }- Parameters:
xClass- Exceptions with causes of this class are wrapped and thrown.xSupplier- Performs wrapping of the execution exception itself when a surrogate cannot be created.- Throws:
X- When cause is an instance ofxClass, throwseewrapped viaxSupplier.X extends Throwable- See Also:
Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable)
-
-