Class ExceptionUtils

  • All Implemented Interfaces:
    Utils

    public abstract class ExceptionUtils
    extends java.lang.Object
    implements Utils
    Exception Utilities class
    Since:
    1.0.0
    Author:
    Mercy
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends java.lang.Throwable>
      T
      create​(java.lang.Class<T> throwableClass)
      Creates a new instance of the specified Throwable class using its default constructor.
      static <T extends java.lang.Throwable>
      T
      create​(java.lang.Class<T> throwableClass, java.lang.Object... args)
      Creates a new instance of the specified Throwable class using constructor arguments.
      static <T extends java.lang.Throwable>
      T
      create​(java.lang.Class<T> throwableClass, java.lang.String message)
      Creates a new instance of the specified Throwable class with the provided detail message.
      static <T extends java.lang.Throwable>
      T
      create​(java.lang.Class<T> throwableClass, java.lang.String message, java.lang.Throwable cause)
      Creates a new instance of the specified Throwable class with the provided detail message and cause.
      static <T extends java.lang.Throwable>
      T
      create​(java.lang.Class<T> throwableClass, java.lang.Throwable cause)
      Creates a new instance of the specified Throwable class with the provided cause.
      static <T extends java.lang.Throwable>
      T
      create​(java.lang.Class<T> throwableClass, java.lang.Throwable cause, java.lang.String messagePattern, java.lang.Object... args)
      Creates a new instance of the specified Throwable class with a formatted detail message and cause.
      static java.lang.String getStackTrace​(java.lang.Throwable throwable)
      Gets the stack trace from a Throwable as a String.
      static <T extends java.lang.Throwable,​TT extends java.lang.Throwable>
      TT
      throwTarget​(T source, java.lang.Class<TT> thrownType)
      Throws the given source exception wrapped into the specified target exception type.
      static <T extends java.lang.Throwable,​TT extends java.lang.Throwable>
      TT
      wrap​(T source, java.lang.Class<TT> thrownType)
      Wraps the given source exception into a new instance of the specified type.
      • Methods inherited from class java.lang.Object

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

      • getStackTrace

        @Nonnull
        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 may 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.

        Example Usage

        
         try {
             // some code that may throw an exception
         } catch (Exception e) {
             String stackTrace = ExceptionUtils.getStackTrace(e);
             System.out.println(stackTrace);
         }
         
        Parameters:
        throwable - the Throwable to be examined
        Returns:
        the stack trace as generated by the exception's printStackTrace(PrintWriter) method
      • wrap

        @Nonnull
        public static <T extends java.lang.Throwable,​TT extends java.lang.Throwable> TT wrap​(T source,
                                                                                                   java.lang.Class<TT> thrownType)

        Wraps the given source exception into a new instance of the specified type.

        If the source exception is already assignable to the target type, it is returned directly. Otherwise, a new instance of the target type is created using suitable constructor arguments derived from the source exception.

        Example Usages

        
         try {
             // some code that throws an IOException
         } catch (IOException e) {
             RuntimeException re = ExceptionUtils.wrap(e, RuntimeException.class);
             throw re;
         }
         
        
         try {
             // some code that throws an IllegalArgumentException
         } catch (IllegalArgumentException e) {
             MyCustomException myEx = ExceptionUtils.wrap(e, MyCustomException.class);
             throw myEx;
         }
         
        Type Parameters:
        T - the type of the source exception
        TT - the type to wrap the source exception into
        Parameters:
        source - the source exception to be wrapped
        thrownType - the class of the target type to wrap into
        Returns:
        an instance of the target type wrapping or referencing the source exception
      • create

        @Nonnull
        public static <T extends java.lang.Throwable> T create​(java.lang.Class<T> throwableClass)

        Creates a new instance of the specified Throwable class using its default constructor.

        Example Usage

        
         try {
             // some code that may throw an exception
         } catch (Exception e) {
             RuntimeException re = ExceptionUtils.create(RuntimeException.class);
             throw re;
         }
         
        Type Parameters:
        T - the type of the Throwable to be created
        Parameters:
        throwableClass - the Class object of the Throwable to instantiate
        Returns:
        a new instance of the specified Throwable
      • create

        @Nonnull
        public static <T extends java.lang.Throwable> T create​(java.lang.Class<T> throwableClass,
                                                               java.lang.String message)

        Creates a new instance of the specified Throwable class with the provided detail message.

        Example Usage

        
         try {
             // some code that may throw an exception
         } catch (Exception e) {
             RuntimeException re = ExceptionUtils.create(RuntimeException.class, "An error occurred");
             throw re;
         }
         
        Type Parameters:
        T - the type of the Throwable to be created
        Parameters:
        throwableClass - the Class object of the Throwable to instantiate
        message - the detail message for the Throwable
        Returns:
        a new instance of the specified Throwable with the provided message
      • create

        @Nonnull
        public static <T extends java.lang.Throwable> T create​(java.lang.Class<T> throwableClass,
                                                               java.lang.Throwable cause)

        Creates a new instance of the specified Throwable class with the provided cause.

        Example Usage

        
         try {
             // some code that may throw an exception
         } catch (Exception e) {
             RuntimeException re = ExceptionUtils.create(RuntimeException.class, e);
             throw re;
         }
         
        Type Parameters:
        T - the type of the Throwable to be created
        Parameters:
        throwableClass - the Class object of the Throwable to instantiate
        cause - the cause (which is usually an exception that was caught)
        Returns:
        a new instance of the specified Throwable with the provided cause
      • create

        @Nonnull
        public static <T extends java.lang.Throwable> T create​(java.lang.Class<T> throwableClass,
                                                               java.lang.String message,
                                                               java.lang.Throwable cause)

        Creates a new instance of the specified Throwable class with the provided detail message and cause.

        Example Usage

        
         try {
             // some code that may throw an exception
         } catch (Exception e) {
             RuntimeException re = ExceptionUtils.create(RuntimeException.class, "An error occurred", e);
             throw re;
         }
         
        Type Parameters:
        T - the type of the Throwable to be created
        Parameters:
        throwableClass - the Class object of the Throwable to instantiate
        message - the detail message for the Throwable
        cause - the cause (which is usually an exception that was caught)
        Returns:
        a new instance of the specified Throwable with the provided message and cause
      • create

        @Nonnull
        public static <T extends java.lang.Throwable> T create​(java.lang.Class<T> throwableClass,
                                                               java.lang.Throwable cause,
                                                               java.lang.String messagePattern,
                                                               java.lang.Object... args)

        Creates a new instance of the specified Throwable class with a formatted detail message and cause.

        This method formats the message using the provided pattern and arguments, then creates a new instance of the target Throwable type with both the formatted message and the cause.

        Example Usage

        
         try {
             // some code that may throw an exception
         } catch (Exception e) {
             RuntimeException re = ExceptionUtils.create(
                 RuntimeException.class,
                 e,
                 "An error occurred with code: %d",
                 500
             );
             throw re;
         }
         
        Type Parameters:
        T - the type of the Throwable to be created
        Parameters:
        throwableClass - the Class object of the Throwable to instantiate
        cause - the cause (which is usually an exception that was caught)
        messagePattern - the pattern used to format the detail message
        args - the arguments used to replace placeholders in the message pattern
        Returns:
        a new instance of the specified Throwable with the formatted message and cause
      • create

        @Nonnull
        public static <T extends java.lang.Throwable> T create​(java.lang.Class<T> throwableClass,
                                                               java.lang.Object... args)

        Creates a new instance of the specified Throwable class using constructor arguments.

        This method attempts to instantiate the target Throwable class by matching and using the provided constructor arguments. It is particularly useful when the exact combination of constructor parameters is known and needs to be dynamically applied.

        Example Usages

        
         try {
             // some code that may throw an exception
         } catch (Exception e) {
             RuntimeException re = ExceptionUtils.create(RuntimeException.class, "Error", e);
             throw re;
         }
         
        
         try {
             // some code that may throw an exception
         } catch (Throwable t) {
             IOException ioEx = ExceptionUtils.create(IOException.class, "File not found");
             throw ioEx;
         }
         
        Type Parameters:
        T - the type of the Throwable to be created
        Parameters:
        throwableClass - the Class object of the Throwable to instantiate
        args - the variable-length list of constructor arguments used for instantiation
        Returns:
        a new instance of the specified Throwable initialized with the given arguments
      • throwTarget

        @Nonnull
        public static <T extends java.lang.Throwable,​TT extends java.lang.Throwable> TT throwTarget​(T source,
                                                                                                          java.lang.Class<TT> thrownType)
                                                                                                   throws TT extends java.lang.Throwable

        Throws the given source exception wrapped into the specified target exception type.

        If the source exception is already assignable to the target type, it will be rethrown directly. Otherwise, a new instance of the target type is created using suitable constructor arguments derived from the source exception.

        Example Usages

        
         try {
             // some code that throws an IOException
         } catch (IOException e) {
             throw ExceptionUtils.throwTarget(e, RuntimeException.class); // Rethrows as RuntimeException
         }
         
        
         try {
             // some code that throws an IllegalArgumentException
         } catch (IllegalArgumentException e) {
             throw ExceptionUtils.throwTarget(e, MyCustomException.class); // Wraps and throws as MyCustomException
         }
         
        Type Parameters:
        T - the type of the source exception
        TT - the type to wrap the source exception into before throwing
        Parameters:
        source - the source exception to be wrapped and thrown
        thrownType - the class of the target type to wrap into
        Returns:
        this method does not return anything; it always throws an exception
        Throws:
        TT - wraps and throws the source or newly created target exception
        TT extends java.lang.Throwable