Package org.eclipse.jetty.util
Class ExceptionUtil
java.lang.Object
org.eclipse.jetty.util.ExceptionUtil
Exception (or rather Throwable
utility methods.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addSuppressedIfNotAssociated
(Throwable throwable, Throwable suppressed) Add a suppressed exception if it is not associated.static boolean
areAssociated
(Throwable t1, Throwable t2) Check if twoThrowable
s are associated.static boolean
areNotAssociated
(Throwable t1, Throwable t2) Check if twoThrowable
s are associated.static <T extends Throwable>
TConvert aThrowable
to a specific type by casting or construction on a new instance.static void
Call a method that handles aThrowable
, catching and associating any exception that it throws.static void
call
(Invocable.Callable callable, Consumer<Throwable> failure) Call aInvocable.Callable
and handle any resulting failuresstatic void
callAndThen
(Runnable call, Runnable then) Call aRunnable
and then always call another, ignoring any exceptions thrown.static void
callAndThen
(Throwable cause, Consumer<Throwable> call, Runnable then) static void
Call a handler ofThrowable
and then always call another, suppressing any exceptions thrown.static Throwable
Combine two, possible null,Throwable
s in a style to facilitate handling multiple exceptions that are accumulated as suppressed exceptions.static <T> T
get
(CompletableFuture<T> completableFuture) Get from aCompletableFuture
and convert any uncheck exceptions toRuntimeException
.static void
ifExceptionThrow
(Throwable throwable) static <T extends Throwable>
voidifExceptionThrowAllAs
(Class<T> type, Throwable throwable) Throw aThrowable
as a specific type, casting or construction as required.static <T extends Throwable>
voidifExceptionThrowAs
(Class<T> type, Throwable throwable) Throw aThrowable
as a specific type, casting or construction as required.static void
ifExceptionThrowUnchecked
(Throwable throwable) static void
Call aRunnable
and handle failuresstatic <T extends Throwable>
TwithSuppressed
(T t, List<Throwable> errors) Decorate a Throwable with the suppressed errors and return it.
-
Method Details
-
as
public static <T extends Throwable> T as(Class<T> type, Throwable throwable) throws IllegalArgumentException Convert a
Throwable
to a specific type by casting or construction on a new instance.- Type Parameters:
T
- The type of theThrowable
to be returned- Parameters:
type
- The type of the Throwable to be thrown. It must have a constructor that takes aThrowable
as a cause.throwable
- TheThrowable
or null- Returns:
- A
Throwable
of typeT
or null. - Throws:
IllegalArgumentException
- if the passedtype
cannot be constructed with a cause.
-
ifExceptionThrow
-
ifExceptionThrowUnchecked
-
ifExceptionThrowAs
public static <T extends Throwable> void ifExceptionThrowAs(Class<T> type, Throwable throwable) throws Error, RuntimeException, T, IllegalArgumentException Throw a
Throwable
as a specific type, casting or construction as required.- Type Parameters:
T
- The type of theThrowable
to be thrown ifthrowable
is not null.- Parameters:
type
- The type of the Throwable to be thrown. It must have a constructor that takes aThrowable
as a cause.throwable
- AThrowable
or null.- Throws:
Error
- If the passedThrowable
is anError
.RuntimeException
- If the passedThrowable
is aRuntimeException
.T
- Thrown inthrowable
is not null and neither anError
norRuntimeException
.IllegalArgumentException
- if the passedtype
cannot be constructed with a cause.
-
ifExceptionThrowAllAs
public static <T extends Throwable> void ifExceptionThrowAllAs(Class<T> type, Throwable throwable) throws T Throw a
Throwable
as a specific type, casting or construction as required.- Type Parameters:
T
- The type of theThrowable
to be thrown ifthrowable
is not null.- Parameters:
type
- The type of the Throwable to be thrown. It must have a constructor that takes aThrowable
as a cause.throwable
- AThrowable
or null.- Throws:
T
- Thrown inthrowable
is not null and neither anError
norRuntimeException
.
-
areAssociated
Check if twoThrowable
s are associated.- Parameters:
t1
- A Throwable or nullt2
- Another Throwable or null- Returns:
- true iff the exceptions are not associated by being the same instance, sharing a cause or one suppressing the other.
-
areNotAssociated
Check if twoThrowable
s are associated.- Parameters:
t1
- A Throwable or nullt2
- Another Throwable or null- Returns:
- true iff the exceptions are not associated by being the same instance, sharing a cause or one suppressing the other.
-
addSuppressedIfNotAssociated
Add a suppressed exception if it is not associated.- Parameters:
throwable
- The main Throwablesuppressed
- The Throwable to suppress if it is not associated.- See Also:
-
withSuppressed
Decorate a Throwable with the suppressed errors and return it.- Type Parameters:
T
- of type Throwable- Parameters:
t
- the throwableerrors
- the list of errors- Returns:
- the original throwable with suppressed errors
-
combine
Combine two, possible null,
Throwable
s in a style to facilitate handling multiple exceptions that are accumulated as suppressed exceptions. This is freqently used in the following pattern:Throwable multiException = null; for (Runnable task : manyThings) { try { task.run(); } catch(Throwable t) { multiException = multiException.combine(multiException, t) } } MultiException.ifExceptionalThrow(multiException);
- Parameters:
t1
- A Throwable or nullt2
- Another Throwable or null- Returns:
- t1 with t2 suppressed, or null.
-
call
Call a method that handles aThrowable
, catching and associating any exception that it throws. -
call
Call aInvocable.Callable
and handle any resulting failures- Parameters:
callable
- TheInvocable.Callable
to callfailure
- A handler of failures from the call- See Also:
-
run
Call aRunnable
and handle failures- Parameters:
runnable
- The runnable to callfailure
- The handling of failures- See Also:
-
callAndThen
Call a handler ofThrowable
and then always call another, suppressing any exceptions thrown. -
callAndThen
-
callAndThen
Call aRunnable
and then always call another, ignoring any exceptions thrown. -
get
Get from a
CompletableFuture
and convert any uncheck exceptions toRuntimeException
.- Type Parameters:
T
- The type of theCompletableFuture
- Parameters:
completableFuture
- The future to get from.- Returns:
- The value from calling
CompletableFuture.get()
- Throws:
RuntimeException
- if the call toCompletableFuture.get()
throws.
-