- java.lang.Object
-
- com.aoapps.lang.Throwables
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ThrowableaddSuppressed(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 booleanisSuppressed(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>
Exwrap(Throwable t, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier)
-
-
-
Method Detail
-
isSuppressed
public static boolean isSuppressed(Throwable t0, Throwable suppressed)
Checks if a throwable is already suppressed.
-
addSuppressed
public static Throwable addSuppressed(Throwable t0, Throwable suppressed)
Adds a suppressed exception, unless already in the list of suppressed exceptions.When
suppressedis anInterruptedExceptionandt0is not anInterruptedException, the current thread will be re-interrupted.When
suppressedis aThreadDeathandt0is not itself aThreadDeath,suppressedwill be returned instead, witht0added to it as suppressed. This is to maintain the precedence ofThreadDeathfor fail-fast behavior.- Parameters:
t0- The throwable to add to. Whennull,suppressedis returned instead.suppressed- The suppressed throwable, skipped whennull- Returns:
t0when not null, otherwisesuppressed.- See Also:
addSuppressedAndThrow(java.lang.Throwable, java.lang.Class, java.util.function.Function, java.lang.Throwable)
-
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, Ex extends Throwable
Adds a suppressed exception, unless already in the list of suppressed exceptions, wrapping when needed, then throwing the result.When
suppressedis anInterruptedExceptionandt0is not anInterruptedException, the current thread will be re-interrupted.When
suppressedis aThreadDeathandt0is not itself aThreadDeath,suppressedwill be returned instead, witht0added to it as suppressed. This is to maintain the precedence ofThreadDeathfor fail-fast behavior.Only returns when both
t0andsuppressedarenull.- Parameters:
t0- The throwable to add to. Whennull,suppressedis thrown instead.exClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.exSupplier- Other throwables are wrapped via this function, then thrownsuppressed- The suppressed throwable, skipped whennull- Throws:
Error- When resolved throwable is anErrorRuntimeException- When resolved throwable is aRuntimeExceptionEx- When resolved throwable is an instance ofexClass, otherwise wrapped viaexSupplierEx extends Throwable- See Also:
addSuppressed(java.lang.Throwable, java.lang.Throwable),wrap(java.lang.Throwable, java.lang.Class, java.util.function.Function)
-
wrap
public static <Ex extends Throwable> Ex wrap(Throwable t, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier) throws Error, RuntimeException
Wraps an exception, unless is an instance ofexClass,Error, orRuntimeException.- When
null, returnsnull. - When is an instance of
exClass, returns the exception. - When is
ErrororRuntimeException, 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
InterruptedExceptionand 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 notErrororRuntimeException, are wrapped via this function, then returned.- Returns:
twhen is an instance ofexClassor whenthas been wrapped viaexSupplier.- Throws:
Error- Whentis anErrorRuntimeException- Whentis aRuntimeException- See Also:
addSuppressedAndThrow(java.lang.Throwable, java.lang.Class, java.util.function.Function, java.lang.Throwable)
- When
-
newSurrogate
public static <Ex extends Throwable> Ex newSurrogate(Ex template, Throwable cause)
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 newThrowableprovides 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 themselvesin static blocks, while other APIs may be registered via theServiceLoadermechanism on theThrowableSurrogateFactoryInitializerinterface.- 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:
ExecutionExceptions
-
newSurrogate
public static <Ex extends Throwable> Ex newSurrogate(Ex template)
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 newThrowableprovides 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 themselvesin static blocks, while other APIs may be registered via theServiceLoadermechanism on theThrowableSurrogateFactoryInitializerinterface.- 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:
ExecutionExceptions
-
registerSurrogateFactory
public static <Ex extends Throwable> void registerSurrogateFactory(Class<Ex> exClass, ThrowableSurrogateFactory<Ex> factory)
Registers a new throwable surrogate factory for a given type.
-
-