ExecutionException
.-
Method Summary
Modifier and TypeMethodDescriptionstatic <Ex extends Throwable>
voidwrapAndThrow
(ExecutionException ee, Class<? extends Ex> exClass, BiFunction<? super String, ? super ExecutionException, ? extends Ex> exSupplier) static <Ex extends Throwable>
voidwrapAndThrowWithTemplate
(ExecutionException ee, Class<? extends Ex> exClass, BiFunction<? super Ex, ? super ExecutionException, ? extends Ex> exSupplier)
-
Method Details
-
wrapAndThrow
public static <Ex extends Throwable> void wrapAndThrow(ExecutionException ee, Class<? extends Ex> exClass, BiFunction<? super String, ? super ExecutionException, throws Ex? extends Ex> exSupplier) Wraps and throws anExecutionException
when its cause is an instance ofexClass
. 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 functionexSupplier
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 viaexSupplier
, and the resulting surrogate is not itself anInterruptedException
, 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 ofexClass
, throwsee
wrapped viaexSupplier
.- See Also:
-
wrapAndThrowWithTemplate
public static <Ex extends Throwable> void wrapAndThrowWithTemplate(ExecutionException ee, Class<? extends Ex> exClass, BiFunction<? super Ex, ? super ExecutionException, throws Ex? extends Ex> exSupplier) Wraps and throws anExecutionException
when its cause is an instance ofexClass
.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 functionexSupplier
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 viaexSupplier
, and the resulting surrogate is not itself anInterruptedException
, 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 ofexClass
, throwsee
wrapped viaexSupplier
.- See Also:
-