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.
    • 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.
    • 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.