Class ExceptionUtil

java.lang.Object
org.eclipse.jetty.util.ExceptionUtil

public class ExceptionUtil extends Object

Exception (or rather Throwable utility methods.

  • 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 the Throwable to be returned
      Parameters:
      type - The type of the Throwable to be thrown. It must have a constructor that takes a Throwable as a cause.
      throwable - The Throwable or null
      Returns:
      A Throwable of type T or null.
      Throws:
      IllegalArgumentException - if the passed type cannot be constructed with a cause.
    • ifExceptionThrow

      public static void ifExceptionThrow(Throwable throwable) throws Error, Exception
      Throw a Throwable as a checked Exception if it cannot be thrown as unchecked.
      Parameters:
      throwable - The Throwable to throw or null.
      Throws:
      Error - If the passed Throwable is an Error.
      Exception - Otherwise, if the passed Throwable is not null.
    • ifExceptionThrowUnchecked

      public static void ifExceptionThrowUnchecked(Throwable throwable) throws Error, RuntimeException
      Throw a Throwable as an unchecked Exception.
      Parameters:
      throwable - The Throwable to throw or null.
      Throws:
      Error - If the passed Throwable is an Error.
      RuntimeException - Otherwise, if the passed Throwable is not null.
    • 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 the Throwable to be thrown if throwable is not null.
      Parameters:
      type - The type of the Throwable to be thrown. It must have a constructor that takes a Throwable as a cause.
      throwable - A Throwable or null.
      Throws:
      Error - If the passed Throwable is an Error.
      RuntimeException - If the passed Throwable is a RuntimeException.
      T - Thrown in throwable is not null and neither an Error nor RuntimeException.
      IllegalArgumentException - if the passed type 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 the Throwable to be thrown if throwable is not null.
      Parameters:
      type - The type of the Throwable to be thrown. It must have a constructor that takes a Throwable as a cause.
      throwable - A Throwable or null.
      Throws:
      T - Thrown in throwable is not null and neither an Error nor RuntimeException.
    • areAssociated

      public static boolean areAssociated(Throwable t1, Throwable t2)
      Check if two Throwables are associated.
      Parameters:
      t1 - A Throwable or null
      t2 - 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

      public static boolean areNotAssociated(Throwable t1, Throwable t2)
      Check if two Throwables are associated.
      Parameters:
      t1 - A Throwable or null
      t2 - 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

      public static void addSuppressedIfNotAssociated(Throwable throwable, Throwable suppressed)
      Add a suppressed exception if it is not associated.
      Parameters:
      throwable - The main Throwable
      suppressed - The Throwable to suppress if it is not associated.
      See Also:
    • withSuppressed

      public static <T extends Throwable> T withSuppressed(T t, List<Throwable> errors)
      Decorate a Throwable with the suppressed errors and return it.
      Type Parameters:
      T - of type Throwable
      Parameters:
      t - the throwable
      errors - the list of errors
      Returns:
      the original throwable with suppressed errors
    • combine

      public static Throwable combine(Throwable t1, Throwable t2)

      Combine two, possible null, Throwables 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 null
      t2 - Another Throwable or null
      Returns:
      t1 with t2 suppressed, or null.
    • call

      public static void call(Throwable cause, Consumer<Throwable> consumer)
      Call a method that handles a Throwable, catching and associating any exception that it throws.
      Parameters:
      cause - The Throwable to pass to the consumer
      consumer - The handler of a Throwable
    • call

      public static void call(Invocable.Callable callable, Consumer<Throwable> failure)
      Call a Invocable.Callable and handle any resulting failures
      Parameters:
      callable - The Invocable.Callable to call
      failure - A handler of failures from the call
      See Also:
    • run

      public static void run(Runnable runnable, Consumer<Throwable> failure)
      Call a Runnable and handle failures
      Parameters:
      runnable - The runnable to call
      failure - The handling of failures
      See Also:
    • callAndThen

      public static void callAndThen(Throwable cause, Consumer<Throwable> call, Consumer<Throwable> then)
      Call a handler of Throwable and then always call another, suppressing any exceptions thrown.
      Parameters:
      cause - The Throwable to be passed to both consumers.
      call - The first Consumer of Throwable to call.
      then - The second Consumer of Throwable to call.
    • callAndThen

      public static void callAndThen(Throwable cause, Consumer<Throwable> call, Runnable then)
      Call a handler of Throwable and then always call a Runnable, suppressing any exceptions thrown.
      Parameters:
      cause - The Throwable to be passed to both consumers.
      call - The Consumer of Throwable to call.
      then - The Runnable to call.
    • callAndThen

      public static void callAndThen(Runnable call, Runnable then)
      Call a Runnable and then always call another, ignoring any exceptions thrown.
      Parameters:
      call - The first Runnable to call.
      then - The second Runnable to call.
    • get

      public static <T> T get(CompletableFuture<T> completableFuture)

      Get from a CompletableFuture and convert any uncheck exceptions to RuntimeException.

      Type Parameters:
      T - The type of the CompletableFuture
      Parameters:
      completableFuture - The future to get from.
      Returns:
      The value from calling CompletableFuture.get()
      Throws:
      RuntimeException - if the call to CompletableFuture.get() throws.