Class Throwables


  • public final class Throwables
    extends Object
    Utilities for working with Throwable.
    • Method Detail

      • isSuppressed

        public static boolean isSuppressed​(Throwable t0,
                                           Throwable suppressed)
        Checks if a throwable is already suppressed.
      • 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 current Thread. 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 new Throwable 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 the ServiceLoader mechanism on the ThrowableSurrogateFactoryInitializer 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:
        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 current Thread. 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 new Throwable 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 the ServiceLoader mechanism on the ThrowableSurrogateFactoryInitializer 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:
        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.