Class ExceptionMethods

java.lang.Object
org.klojang.util.ExceptionMethods

public final class ExceptionMethods extends Object
Methods related to exception handling.
  • Method Details

    • getRootCause

      public static Throwable getRootCause(Throwable exc)
      Returns the root cause of the specified Throwable, or the Throwable itself if it has no cause.
      Parameters:
      exc - the exception whose root cause to retrieve
      Returns:
      the root cause of the exception
    • getRootStackTraceAsString

      public static String getRootStackTraceAsString(Throwable exc)
      Returns the stack trace of the root cause of the specified Throwable as a String.
      Parameters:
      exc - the exception
      Returns:
      the root stack trace as a string
    • getRootStackTraceAsString

      public static String getRootStackTraceAsString(Throwable exc, String... filter)
      Returns the exception message and stack trace of the root cause of the specified Throwable, using the specified string(s) to filter stack trace elements. The stack trace is filtered using a simple String.contains on the className property of the StackTraceElement.
      Parameters:
      exc - the exception
      filter - one or more filters on stack trace elements
      Returns:
      the root stack trace as a string
    • getRootStackTrace

      public static StackTraceElement[] getRootStackTrace(Throwable exc, String... filter)
      Returns the stack trace of the root cause of the specified exception, using the specified string(s) to filter stack trace elements. If the class name of the stack trace element contains the filter string, the stack trace element will be included in the returned array.
      Parameters:
      exc - the exception
      filter - One or more filters on stack trace elements
      Returns:
      the root stack trace
    • getDetailedMessage

      public static String getDetailedMessage(Throwable exc, String search)
      Provides a detailed exception message that includes the class, method and line number of the first statement in the specified exception's stack trace that matches the search string. Note that this may not be the absolute origin of the exception - the statement from which the exception was thrown. If the search term does happen to hit upon the absolute origin of the exception, the detailed exception message will include a notification to that effect. Otherwise the class, method and line number of the statement that did throw the exception is also included in the detailed exception message.
      
       try {
         // stuff ...
       } catch (Exception e) {
         // Log exception message plus class and line number within the
         // com.mycompany code base where things flew off the rails
         logger.error(new ExceptionOrigin(e, "com.mycompany").getDetailedMessage());
       }
       
      Parameters:
      exc - the exception to extract the extra information from
      search - The (partial) name of the package or class you want to zoom in on
      Returns:
      A detailed exception message
      See Also:
    • wrap

      public static RuntimeException wrap(Throwable exc)
      Returns the specified throwable if it already is a RuntimeException, else a RuntimeException wrapping the throwable.
      Parameters:
      exc - a checked or unchecked exception
      Returns:
      the specified throwable or a RuntimeException wrapping it
    • wrap

      public static RuntimeException wrap(Throwable exc, String customMessage, Object... msgArgs)
      Returns the specified throwable if it already is a RuntimeException, else a RuntimeException wrapping the throwable.
      Parameters:
      exc - a checked or unchecked exception
      customMessage - a custom message passed on to the RuntimeException wrapping the original exception
      msgArgs - the String.format message arguments to the custom message
      Returns:
      the specified throwable or a RuntimeException wrapping it
    • wrap

      public static <T extends RuntimeException> RuntimeException wrap(Throwable exc, Function<Throwable,T> exceptionFactory)
      Returns the specified throwable if it already is a RuntimeException, else the RuntimeException returned by the specified function. The function wis given the original exception (exc) as argument.
      Type Parameters:
      T - the type of the RuntimeException
      Parameters:
      exc - the exception to be wrapped if it is not a RuntimeException
      exceptionFactory - a function that converts the specified throwable into a RuntimeException, typically a method reference (e.g. IOException::new)
      Returns:
      the specified throwable or a RuntimeException wrapping it
    • wrap

      public static <T extends RuntimeException> RuntimeException wrap(Throwable exception, BiFunction<String,Throwable,T> exceptionFactory, String customMessage, Object... msgArgs)
      Returns the specified throwable if it already is a RuntimeException, else a RuntimeException produced by the specified function. For example:
      
       try {
        // stuff ...
       } catch(Throwable t) {
        throw ExceptionMethods.wrap(t, "Bad stuff happening", IllegalStateException::new);
       }
       
      Type Parameters:
      T - The type of the RuntimeException
      Parameters:
      exception - the exception to be wrapped if it is not a RuntimeException
      exceptionFactory - the producer of the RuntimeException, typically the two-argument constructor of an Exception that takes a String argument and a Throwable argument
      customMessage - a custom message passed on to the RuntimeException wrapping the original exception
      msgArgs - the String.format message arguments to the custom message
      Returns:
      the specified throwable or a RuntimeException wrapping it
    • uncheck

      public static RuntimeException uncheck(Throwable exc)
      Returns the specified throwable if it already is a RuntimeException, else an UncheckedException wrapping the throwable. This method is primarily meant to "uncheck" checked exceptions that you cannot in practice properly deal with, and are therefore, for all practical purposes, a runtime exception. For example an IOException which is documented as being thrown "if an I/O error occurs".
      Parameters:
      exc - a checked or unchecked exception
      Returns:
      the provided Throwable or an UncheckedException wrapping it
      See Also:
    • uncheck

      public static RuntimeException uncheck(Throwable exc, String customMessage)
      Returns the specified throwable if it already is a RuntimeException, else an UncheckedException wrapping the throwable. This method is primarily meant to "uncheck" checked exceptions that you cannot in practice properly deal with, and are therefore, for all practical purposes, a runtime exception. For example an IOException which is documented as being thrown "if an I/O error occurs".
      Parameters:
      exc - a checked or unchecked exception
      customMessage - a custom message to pass to the constructor of UncheckedException
      Returns:
      the provided Throwable or an UncheckedException wrapping it
      See Also:
    • rootCause

      public static RuntimeException rootCause(Throwable exc)
      Returns the specified throwable if it already is a RuntimeException, else an RootException exception wrapping the root cause of the provided exception.
      Parameters:
      exc - a checked or unchecked exception
      Returns:
      the provided Throwable or an UncheckedException wrapping it
      See Also:
    • rootCause

      public static RuntimeException rootCause(Throwable exc, String customMessage)
      Returns the specified throwable if it already is a RuntimeException, else an RootException exception wrapping the root cause of the provided exception.
      Parameters:
      exc - a checked or unchecked exception
      customMessage - a custom message to pass to the constructor of UncheckedException
      Returns:
      the provided Throwable or a RootException wrapping its root cause
      See Also: