Class ExceptionUtilities

java.lang.Object
com.strobel.core.ExceptionUtilities

public final class ExceptionUtilities extends Object
  • Constructor Details

    • ExceptionUtilities

      public ExceptionUtilities()
  • Method Details

    • asRuntimeException

      public static RuntimeException asRuntimeException(Throwable t)
    • unwrap

      public static Throwable unwrap(Throwable t)
    • getMessage

      public static String getMessage(Throwable t)
    • getStackTraceString

      public static String getStackTraceString(Throwable t)
    • rethrowCritical

      public static void rethrowCritical(Throwable t)
      Rethrows the specified exception only if it is within a narrow subset of 'critical' exceptions, e.g., ThreadDeath or VirtualMachineError.
    • rethrow

      public static <T extends Throwable> RuntimeException rethrow(Throwable t) throws T

      Sneakily rethrows any exception without the compiler complaining if the exception is checked but unhandled. The signature declares a return type of RuntimeException, for cases where the caller method must exit to satisfy control flow requirements (e.g., final variable assignment), but this method never actually returns a value.

      Trivial example:

      void doSomething() {
          try {
              mightThrowCheckedException();
          }
          catch (final Throwable t) {
              throw ExceptionUtilities.rethrow(t);
          }
      }

      Example requiring a return value:

      T returnSomething() {
          try {
              return mightThrowCheckedException();
          }
          catch (final Throwable t) {
              // The call below always throws, but the compiler doesn't know that and demands
              // we either return a value or throw.  The return value, while never used, allows
              // us to satisfying the compiler by exiting the current method exceptionally.
              throw ExceptionUtilities.rethrow(t);
          }
      }

      Example with constructor and final fields:

      class U {
          final T mustBeAssigned;
      
          U() {
              try {
                  mustBeAssigned = mightThrowCheckedException();
              }
              catch (final Throwable t) {
                  // The compiler requires us to definitively assign all final fields before
                  // returning or throw.  Since the compiler doesn't know that the call below
                  // always throws, we can throw the dummy result to satisfy the compiler.
                  throw ExceptionUtilities.rethrow(t);
              }
          }
      }
      Returns:
      This method will never return a value; it always throws.
      Throws:
      T - This method rethrows the original exception t, or a NullPointerException if t is null.
    • rethrowAs

      public static <T extends Throwable, R> R rethrowAs(Throwable t) throws T

      Equivalent to rethrow, but with an open-ended return type, allowing calls to this method to be used as the body of lambda expressions that must return a specific type.

      Example:

      public static <T, R> Function<T, R> throwing(final Throwable t) {
          return _ -> ExceptionUtilities.rethrowAs(t);
      }
      Returns:
      This method will never return a value; it always throws.
      Throws:
      T - This method rethrows the original exception t, or a NullPointerException if t is null.
    • wrapOrThrow

      public static RuntimeException wrapOrThrow(Throwable t)