Class ExceptionUtils


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

    Created: 2017. 10. 7.

    Since:
    5.0.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ExceptionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Throwable getRootCause​(java.lang.Throwable t)
      Method that can be used to find the "root cause", innermost of chained (wrapped) exceptions.
      static java.lang.String getStacktrace​(java.lang.Throwable throwable)
      Gets the stack trace from a Throwable as a String.
      static java.lang.IllegalArgumentException throwAsIAE​(java.lang.Throwable t)
      Method that will wrap 't' as an IllegalArgumentException if it is a checked exception; otherwise (runtime exception or error) throw as is.
      static java.lang.IllegalArgumentException throwAsIAE​(java.lang.Throwable t, java.lang.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.
      static java.lang.Throwable throwIfError​(java.lang.Throwable t)
      Helper method that will check if argument is an Error, and if so, (re)throw it; otherwise just return.
      static java.lang.Throwable throwIfIOE​(java.lang.Throwable t)
      Helper method that will check if argument is an IOException, and if so, (re)throw it; otherwise just return.
      static java.lang.Throwable throwIfRTE​(java.lang.Throwable t)
      Helper method that will check if argument is an RuntimeException, and if so, (re)throw it; otherwise just return.
      static java.lang.Throwable throwRootCauseIfIOE​(java.lang.Throwable t)
      Method that works like by calling getRootCause(java.lang.Throwable) and then either throwing it (if instanceof IOException), or return.
      static java.lang.IllegalArgumentException unwrapAndThrowAsIAE​(java.lang.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.
      static java.lang.IllegalArgumentException unwrapAndThrowAsIAE​(java.lang.Throwable t, java.lang.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExceptionUtils

        public ExceptionUtils()
    • Method Detail

      • getRootCause

        public static java.lang.Throwable getRootCause​(java.lang.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
      • getStacktrace

        public static java.lang.String getStacktrace​(java.lang.Throwable throwable)
        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:
        throwable - the Throwable to be examined
        Returns:
        the stack trace as generated by the exception's printStackTrace(PrintWriter) method
      • throwIfError

        public static java.lang.Throwable throwIfError​(java.lang.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 java.lang.Throwable throwIfRTE​(java.lang.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 java.lang.Throwable throwIfIOE​(java.lang.Throwable t)
                                              throws java.io.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:
        java.io.IOException - rethrow the IOException
      • throwRootCauseIfIOE

        public static java.lang.Throwable throwRootCauseIfIOE​(java.lang.Throwable t)
                                                       throws java.io.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:
        java.io.IOException - rethrow the IOException
      • throwAsIAE

        public static java.lang.IllegalArgumentException throwAsIAE​(java.lang.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 java.lang.IllegalArgumentException throwAsIAE​(java.lang.Throwable t,
                                                                    java.lang.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 java.lang.IllegalArgumentException unwrapAndThrowAsIAE​(java.lang.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 java.lang.IllegalArgumentException unwrapAndThrowAsIAE​(java.lang.Throwable t,
                                                                             java.lang.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