Class Throwables


  • public final class Throwables
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Throwables()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean anyCauseMatches​(java.lang.Throwable t, java.util.function.Predicate<java.lang.Throwable> cause)  
      static void assertAnyCause​(java.lang.Throwable err, java.lang.Class<? extends java.lang.Throwable> cause)  
      static java.lang.RuntimeException cleaned​(java.lang.Throwable t)
      A shortcut for unchecked(unwrapped(t)).
      static java.lang.Throwable close​(java.lang.Throwable accumulate, java.lang.AutoCloseable... closeables)
      static java.lang.Throwable close​(java.lang.Throwable accumulate, java.lang.Iterable<? extends java.lang.AutoCloseable> closeables)
      Closes all the resources in the provided collections and accumulates the possible exceptions thrown when closing.
      static void closeAndAddSuppressed​(java.lang.Throwable t, java.lang.AutoCloseable... closeables)
      static void closeAndAddSuppressed​(java.lang.Throwable accumulate, java.lang.Iterable<java.lang.AutoCloseable> closeables)
      Closes all closables in the provided collections and accumulates the possible exceptions thrown when closing.
      static void closeNonNullAndAddSuppressed​(java.lang.Throwable t, java.lang.AutoCloseable... closeables)
      Do what closeAndAddSuppressed(Throwable, Iterable) does, additionally filtering out all null closables.
      static java.util.Optional<java.io.IOException> extractIOExceptionCause​(java.lang.Throwable t)  
      static <T extends java.lang.Throwable>
      boolean
      failIfCanCast​(java.lang.Throwable fail, java.lang.Class<T> checked)  
      static boolean isCausedBy​(java.lang.Throwable t, java.util.function.Predicate<java.lang.Throwable> cause)  
      static void maybeFail​(java.lang.Throwable fail)  
      static <T extends java.lang.Throwable>
      void
      maybeFail​(java.lang.Throwable fail, java.lang.Class<T> checked)  
      static <E extends java.lang.Exception>
      void
      maybeFail​(Throwables.DiscreteAction<? extends E>... actions)  
      static <T extends java.lang.Throwable>
      T
      merge​(T existingFail, T newFail)  
      static void perform​(java.lang.String filePath, Throwables.FileOpType opType, Throwables.DiscreteAction<? extends java.io.IOException>... actions)  
      static java.lang.Throwable perform​(java.lang.Throwable accumulate, java.lang.String filePath, Throwables.FileOpType opType, java.util.stream.Stream<Throwables.DiscreteAction<? extends java.io.IOException>> actions)  
      static java.lang.Throwable perform​(java.lang.Throwable accumulate, java.lang.String filePath, Throwables.FileOpType opType, Throwables.DiscreteAction<? extends java.io.IOException>... actions)  
      static java.lang.Throwable perform​(java.lang.Throwable accumulate, java.util.Iterator<? extends Throwables.DiscreteAction<?>> actions)  
      static java.lang.Throwable perform​(java.lang.Throwable accumulate, java.util.stream.Stream<? extends Throwables.DiscreteAction<?>> actions)  
      static java.lang.Throwable perform​(java.lang.Throwable accumulate, Throwables.DiscreteAction<?>... actions)  
      static <E extends java.lang.Exception>
      void
      perform​(java.util.stream.Stream<? extends Throwables.DiscreteAction<? extends E>> stream, Throwables.DiscreteAction<? extends E>... extra)  
      static <E extends java.lang.Exception>
      void
      perform​(java.util.stream.Stream<Throwables.DiscreteAction<? extends E>> actions)  
      static void perform​(File against, Throwables.FileOpType opType, Throwables.DiscreteAction<? extends java.io.IOException>... actions)  
      static <E extends java.lang.Exception>
      void
      perform​(Throwables.DiscreteAction<? extends E>... actions)  
      static java.lang.RuntimeException throwAsUncheckedException​(java.lang.Throwable t)
      throw the exception as a unchecked exception, wrapping if a checked exception, else rethroing as is.
      static java.lang.RuntimeException unchecked​(java.lang.Throwable t)
      If the provided exception is unchecked, return it directly, otherwise wrap it into a RuntimeException to make it unchecked.
      static java.lang.Throwable unwrapped​(java.lang.Throwable t)
      If the provided throwable is a "wrapping" exception (see below), return the cause of that throwable, otherwise return its argument untouched.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Throwables

        public Throwables()
    • Method Detail

      • isCausedBy

        public static boolean isCausedBy​(java.lang.Throwable t,
                                         java.util.function.Predicate<java.lang.Throwable> cause)
      • anyCauseMatches

        public static boolean anyCauseMatches​(java.lang.Throwable t,
                                              java.util.function.Predicate<java.lang.Throwable> cause)
      • merge

        public static <T extends java.lang.Throwable> T merge​(T existingFail,
                                                              T newFail)
      • maybeFail

        public static void maybeFail​(java.lang.Throwable fail)
      • maybeFail

        public static <T extends java.lang.Throwable> void maybeFail​(java.lang.Throwable fail,
                                                                     java.lang.Class<T> checked)
                                                              throws T extends java.lang.Throwable
        Throws:
        T extends java.lang.Throwable
      • failIfCanCast

        public static <T extends java.lang.Throwable> boolean failIfCanCast​(java.lang.Throwable fail,
                                                                            java.lang.Class<T> checked)
                                                                     throws T extends java.lang.Throwable
        Throws:
        T extends java.lang.Throwable
      • maybeFail

        @SafeVarargs
        public static <E extends java.lang.Exception> void maybeFail​(Throwables.DiscreteAction<? extends E>... actions)
      • perform

        @SafeVarargs
        public static <E extends java.lang.Exception> void perform​(Throwables.DiscreteAction<? extends E>... actions)
                                                            throws E extends java.lang.Exception
        Throws:
        E extends java.lang.Exception
      • perform

        public static <E extends java.lang.Exception> void perform​(java.util.stream.Stream<? extends Throwables.DiscreteAction<? extends E>> stream,
                                                                   Throwables.DiscreteAction<? extends E>... extra)
                                                            throws E extends java.lang.Exception
        Throws:
        E extends java.lang.Exception
      • perform

        public static <E extends java.lang.Exception> void perform​(java.util.stream.Stream<Throwables.DiscreteAction<? extends E>> actions)
                                                            throws E extends java.lang.Exception
        Throws:
        E extends java.lang.Exception
      • perform

        public static java.lang.Throwable perform​(java.lang.Throwable accumulate,
                                                  Throwables.DiscreteAction<?>... actions)
      • perform

        public static java.lang.Throwable perform​(java.lang.Throwable accumulate,
                                                  java.util.stream.Stream<? extends Throwables.DiscreteAction<?>> actions)
      • perform

        public static java.lang.Throwable perform​(java.lang.Throwable accumulate,
                                                  java.util.Iterator<? extends Throwables.DiscreteAction<?>> actions)
      • perform

        @SafeVarargs
        public static java.lang.Throwable perform​(java.lang.Throwable accumulate,
                                                  java.lang.String filePath,
                                                  Throwables.FileOpType opType,
                                                  Throwables.DiscreteAction<? extends java.io.IOException>... actions)
      • perform

        public static java.lang.Throwable perform​(java.lang.Throwable accumulate,
                                                  java.lang.String filePath,
                                                  Throwables.FileOpType opType,
                                                  java.util.stream.Stream<Throwables.DiscreteAction<? extends java.io.IOException>> actions)
      • closeNonNullAndAddSuppressed

        public static void closeNonNullAndAddSuppressed​(@Nonnull
                                                        java.lang.Throwable t,
                                                        java.lang.AutoCloseable... closeables)
        Do what closeAndAddSuppressed(Throwable, Iterable) does, additionally filtering out all null closables.
      • closeAndAddSuppressed

        public static void closeAndAddSuppressed​(@Nonnull
                                                 java.lang.Throwable accumulate,
                                                 java.lang.Iterable<java.lang.AutoCloseable> closeables)
        Closes all closables in the provided collections and accumulates the possible exceptions thrown when closing.
        Parameters:
        accumulate - non-null exception to accumulate errors thrown when closing the provided resources
        closeables - closeables to be closed
      • close

        public static java.lang.Throwable close​(java.lang.Throwable accumulate,
                                                java.lang.AutoCloseable... closeables)
      • close

        public static java.lang.Throwable close​(java.lang.Throwable accumulate,
                                                java.lang.Iterable<? extends java.lang.AutoCloseable> closeables)
        Closes all the resources in the provided collections and accumulates the possible exceptions thrown when closing.
        Parameters:
        accumulate - the initial value for the exception accumulator, can be null
        closeables - closeables to be closed
        Returns:
        null, {@param accumulate} or the first exception thrown when closing the provided resources
      • extractIOExceptionCause

        public static java.util.Optional<java.io.IOException> extractIOExceptionCause​(java.lang.Throwable t)
      • unwrapped

        public static java.lang.Throwable unwrapped​(java.lang.Throwable t)
        If the provided throwable is a "wrapping" exception (see below), return the cause of that throwable, otherwise return its argument untouched.

        We call a "wrapping" exception in the context of that method an exception whose only purpose is to wrap another exception, and currently this method recognize only 2 exception as "wrapping" ones: ExecutionException and CompletionException.

      • unchecked

        public static java.lang.RuntimeException unchecked​(java.lang.Throwable t)
        If the provided exception is unchecked, return it directly, otherwise wrap it into a RuntimeException to make it unchecked.
      • throwAsUncheckedException

        public static java.lang.RuntimeException throwAsUncheckedException​(java.lang.Throwable t)
        throw the exception as a unchecked exception, wrapping if a checked exception, else rethroing as is.
      • cleaned

        public static java.lang.RuntimeException cleaned​(java.lang.Throwable t)
        A shortcut for unchecked(unwrapped(t)). This is called "cleaned" because this basically removes the annoying cruft surrounding an exception :).
      • assertAnyCause

        public static void assertAnyCause​(java.lang.Throwable err,
                                          java.lang.Class<? extends java.lang.Throwable> cause)