Throwable
.-
Method Summary
Modifier and TypeMethodDescriptionstatic Throwable
addSuppressed
(Throwable t0, Throwable suppressed) Adds a suppressed exception, unless already in the list of suppressed exceptions.static <Ex extends Throwable>
voidaddSuppressedAndThrow
(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable, ? extends Ex> exSupplier, Throwable suppressed) Adds a suppressed exception, unless already in the list of suppressed exceptions, wrapping when needed, then throwing the result.static boolean
isSuppressed
(Throwable t0, Throwable suppressed) Checks if a throwable is already suppressed.static <Ex extends Throwable>
ExnewSurrogate
(Ex template) static <Ex extends Throwable>
ExnewSurrogate
(Ex template, Throwable cause) static <Ex extends Throwable>
voidregisterSurrogateFactory
(Class<Ex> exClass, ThrowableSurrogateFactory<Ex> factory) Registers a new throwable surrogate factory for a given type.static <Ex extends Throwable>
Ex
-
Method Details
-
isSuppressed
Checks if a throwable is already suppressed. -
addSuppressed
Adds a suppressed exception, unless already in the list of suppressed exceptions.When
suppressed
is anInterruptedException
andt0
is not anInterruptedException
, the current thread will be re-interrupted.When
suppressed
is aThreadDeath
andt0
is not itself aThreadDeath
,suppressed
will be returned instead, witht0
added to it as suppressed. This is to maintain the precedence ofThreadDeath
for fail-fast behavior.- Parameters:
t0
- The throwable to add to. Whennull
,suppressed
is returned instead.suppressed
- The suppressed throwable, skipped whennull
- Returns:
t0
when not null, otherwisesuppressed
.- See Also:
-
addSuppressedAndThrow
public static <Ex extends Throwable> void addSuppressedAndThrow(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable, ? extends Ex> exSupplier, Throwable suppressed) throws Error, RuntimeException, ExAdds a suppressed exception, unless already in the list of suppressed exceptions, wrapping when needed, then throwing the result.When
suppressed
is anInterruptedException
andt0
is not anInterruptedException
, the current thread will be re-interrupted.When
suppressed
is aThreadDeath
andt0
is not itself aThreadDeath
,suppressed
will be returned instead, witht0
added to it as suppressed. This is to maintain the precedence ofThreadDeath
for fail-fast behavior.Only returns when both
t0
andsuppressed
arenull
.- Parameters:
t0
- The throwable to add to. Whennull
,suppressed
is thrown instead.exClass
- Throwables of this class, as well asError
andRuntimeException
, are thrown directly.exSupplier
- Other throwables are wrapped via this function, then thrownsuppressed
- The suppressed throwable, skipped whennull
- Throws:
Error
- When resolved throwable is anError
RuntimeException
- When resolved throwable is aRuntimeException
Ex
- When resolved throwable is an instance ofexClass
, otherwise wrapped viaexSupplier
- See Also:
-
wrap
public static <Ex extends Throwable> Ex wrap(Throwable t, Class<? extends Ex> exClass, Function<? super Throwable, ? extends Ex> exSupplier) throws Error, RuntimeExceptionWraps an exception, unless is an instance ofexClass
,Error
, orRuntimeException
.- When
null
, returnsnull
. - When is an instance of
exClass
, returns the exception. - When is
Error
orRuntimeException
, throws the exception directly. - Otherwise, throws the exception wrapped via
exSupplier
.
This is expected to typically used within a catch block, to throw a narrower scope:
try { … } catch (Throwable t) { throw Throwables.wrap(t, SQLException.class, SQLException::new); }
When the exception is an
InterruptedException
and is wrapped viaexSupplier
, and the resulting wrapper is not itself anInterruptedException
, the current thread will be re-interrupted.- Parameters:
t
- The throwable to return, throw, or wrap and return.exClass
- Throwables of this class are returned directly.exSupplier
- Throwables that a not returned directly, and are notError
orRuntimeException
, are wrapped via this function, then returned.- Returns:
t
when is an instance ofexClass
or whent
has been wrapped viaexSupplier
.- Throws:
Error
- Whent
is anError
RuntimeException
- Whent
is aRuntimeException
- See Also:
- When
-
newSurrogate
Attempts to create a new instance of the same class as the given template
Throwable
, with all important state maintained, but with the stack trace of the currentThread
. When a new throwable is created, it will use the given cause, possibly with additional wrapping for compatibility.This is used to maintain exception types and states across thread boundaries, such as when an exception cause is obtained from an
ExecutionException
. By wrapping the template, the full stack traces of both threads are maintained. This newThrowable
provides the full stack trace of the caller, while the template contains the stack trace from the other thread.Only types with registered factories will perform the conversion, otherwise the given cause is returned without wrapping.
This current implementation has registered all possible Java SE 8 throwable types, except those that have been removed through Java 17. Additionally, various throwable implementations
register themselves
in static blocks, while other APIs may be registered via theServiceLoader
mechanism on theThrowableSurrogateFactoryInitializer
interface.- Parameters:
cause
- The cause to use for the new throwable. This should typically be either the template itself, or should have the template somewhere in its chain of causes.- Returns:
- When wrapping performed, returns a new throwable of the same class as the template, but with the caller's stack trace and the given cause. When no wrapping performed, returns the template itself.
- See Also:
-
newSurrogate
Attempts to create a new instance of the same class as the given template
Throwable
, with all important state maintained, but with the stack trace of the currentThread
. When a new throwable is created, the template will be used as its cause, possibly with additional wrapping for compatibility.This is used to maintain exception types and states across thread boundaries, such as when an exception cause is obtained from an
ExecutionException
. By wrapping the template, the full stack traces of both threads are maintained. This newThrowable
provides the full stack trace of the caller, while the template contains the stack trace from the other thread.Only types with registered factories will perform the conversion, otherwise the given cause is returned without wrapping.
This current implementation has registered all possible Java SE 8 throwable types, except those that have been removed through Java 17. Additionally, various throwable implementations
register themselves
in static blocks, while other APIs may be registered via theServiceLoader
mechanism on theThrowableSurrogateFactoryInitializer
interface.- Returns:
- When wrapping performed, returns a new throwable of the same class as the template, but with the caller's stack trace and the template as a cause. When no wrapping performed, returns the template itself.
- See Also:
-
registerSurrogateFactory
public static <Ex extends Throwable> void registerSurrogateFactory(Class<Ex> exClass, ThrowableSurrogateFactory<Ex> factory) Registers a new throwable surrogate factory for a given type.
-