Class ExceptionUtils

java.lang.Object
com.aspectran.utils.ExceptionUtils

public class ExceptionUtils extends Object
Provides utilities for manipulating and examining Throwable objects.

Created: 2017. 10. 7.

Since:
5.0.0
  • Constructor Details

    • ExceptionUtils

      public ExceptionUtils()
  • Method Details

    • getRootCause

      @NonNull public static Throwable getRootCause(@NonNull Throwable t)
      Method that can be used to find the "root cause", innermost of chained (wrapped) exceptions.
      Parameters:
      t - the Throwable to possibly propagate
      Returns:
      the root cause
    • getRootCauseException

      @NonNull public static Exception getRootCauseException(@NonNull Exception e)
    • getStacktrace

      @NonNull public static String getStacktrace(@NonNull Throwable t)
      Gets the stack trace from a Throwable as a String.

      The result of this method vary by JDK version as this method uses Throwable.printStackTrace(java.io.PrintWriter). On JDK1.3 and earlier, the cause exception will not be shown unless the specified throwable alters printStackTrace.

      Parameters:
      t - the Throwable to be examined
      Returns:
      the stack trace as generated by the exception's printStackTrace(PrintWriter) method
    • getSimpleMessage

      @NonNull public static String getSimpleMessage(@NonNull Throwable t)
    • getRootCauseSimpleMessage

      @NonNull public static String getRootCauseSimpleMessage(@NonNull Throwable t)
    • throwIfError

      public static Throwable throwIfError(Throwable t)
      Helper method that will check if argument is an Error, and if so, (re)throw it; otherwise just return.
      Parameters:
      t - the Throwable to possibly propagate
      Returns:
      the Throwable
    • throwIfRTE

      public static Throwable throwIfRTE(Throwable t)
      Helper method that will check if argument is an RuntimeException, and if so, (re)throw it; otherwise just return.
      Parameters:
      t - the Throwable to possibly propagate
      Returns:
      the Throwable
    • throwIfIOE

      public static Throwable throwIfIOE(Throwable t) throws IOException
      Helper method that will check if argument is an IOException, and if so, (re)throw it; otherwise just return.
      Parameters:
      t - the Throwable to possibly propagate
      Returns:
      the Throwable
      Throws:
      IOException - rethrow the IOException
    • throwRootCauseIfIOE

      public static Throwable throwRootCauseIfIOE(Throwable t) throws IOException
      Method that works like by calling getRootCause(java.lang.Throwable) and then either throwing it (if instanceof IOException), or return.
      Parameters:
      t - the Throwable to possibly propagate
      Returns:
      the Throwable
      Throws:
      IOException - rethrow the IOException
    • throwAsIAE

      public static IllegalArgumentException throwAsIAE(Throwable t)
      Method that will wrap 't' as an IllegalArgumentException if it is a checked exception; otherwise (runtime exception or error) throw as is.
      Parameters:
      t - the Throwable to possibly propagate
    • throwAsIAE

      public static IllegalArgumentException throwAsIAE(Throwable t, String msg)
      Method that will wrap 't' as an IllegalArgumentException (and with specified message) if it is a checked exception; otherwise (runtime exception or error) throw as is.
      Parameters:
      t - the Throwable to possibly propagate
      msg - the detail message
    • unwrapAndThrowAsIAE

      public static IllegalArgumentException unwrapAndThrowAsIAE(Throwable t)
      Method that will locate the innermost exception for given Throwable; and then wrap it as an IllegalArgumentException if it is a checked exception; otherwise (runtime exception or error) throw as is.
      Parameters:
      t - the Throwable to possibly propagate
    • unwrapAndThrowAsIAE

      public static IllegalArgumentException unwrapAndThrowAsIAE(Throwable t, String msg)
      Method that will locate the innermost exception for given Throwable; and then wrap it as an IllegalArgumentException if it is a checked exception; otherwise (runtime exception or error) throw as is.
      Parameters:
      t - the Throwable to possibly propagate
      msg - the detail msg