Class ThrowableUtils

java.lang.Object
org.apache.juneau.common.internal.ThrowableUtils

public class ThrowableUtils extends Object
Various utility methods for creating and working with throwables.
  • Constructor Details

  • Method Details

    • asRuntimeException

      Creates a new RuntimeException.
      Parameters:
      cause - The caused-by exception.
      Returns:
      A new RuntimeException, or the same exception if it's already of that type.
    • cast

      public static <T> T cast(Class<T> type, Throwable t)
      Casts or wraps the specified throwable to the specified type.
      Type Parameters:
      T - The class to cast to.
      Parameters:
      type - The class to cast to.
      t - The throwable to cast.
      Returns:
      Either the same exception if it's already the specified type, or a wrapped exception.
    • getCause

      public static <T extends Throwable> T getCause(Class<T> c, Throwable t)
      Same as Throwable.getCause() but searches the throwable chain for an exception of the specified type.
      Type Parameters:
      T - The throwable type to search for.
      Parameters:
      c - The throwable type to search for.
      t - The throwable to search.
      Returns:
      The exception, or null if not found.
    • safeSupplier

      public static <T> T safeSupplier(ThrowableUtils.SupplierWithThrowable<T> supplier)
      Allows you to wrap a supplier that throws an exception so that it can be used in a fluent interface.
      Type Parameters:
      T - The supplier type.
      Parameters:
      supplier - The supplier throwing an exception.
      Returns:
      The supplied result.
      Throws:
      RuntimeException - if supplier threw an exception.
    • safeRun

      public static void safeRun(Snippet snippet)
      Runs a snippet of code and encapsulates any throwable inside a RuntimeException.
      Parameters:
      snippet - The snippet of code to run.
    • getStackTrace

      public static String getStackTrace(Throwable t)
      Convenience method for getting a stack trace as a string.
      Parameters:
      t - The throwable to get the stack trace from.
      Returns:
      The same content that would normally be rendered via t.printStackTrace()
    • hash

      public static int hash(Throwable t, String stopClass)
      Calculates a 16-bit hash for the specified throwable based on it's stack trace.
      Parameters:
      t - The throwable to calculate the stack trace on.
      stopClass - Optional stop class on which to stop calculation of a stack trace beyond when found.
      Returns:
      A calculated hash.