Class ExceptionUtility

java.lang.Object
org.refcodes.exception.ExceptionUtility

public final class ExceptionUtility extends Object
Utility class for simplifying the work with exceptions.
  • Method Details

    • toMessage

      public static String toMessage(Throwable aThrowable)
      Generates an exception message from the given exception.
      Parameters:
      aThrowable - The exception from which to create the message.
      Returns:
      A message with the (root) cause of the exception (if any).
    • toStackTrace

      public static String toStackTrace(Throwable aThrowable)
      Retrieves a stack trace from the given exception.
      Parameters:
      aThrowable - The exception from which to get the stack trace.
      Returns:
      The stack trace from the exception.
    • toException

      public static Throwable toException(Collection<?> aExceptionProperty)
      Iterates through the objects passed in the collection and tests them whether them implement the ExceptionAccessor. The first one found implementing the ExceptionAccessor and returning an exception (instead of null), than this exception is returned.
      Parameters:
      aExceptionProperty - The objects to test whether them implement the ExceptionAccessor.
      Returns:
      The first exception retrieved from the first object implementing the ExceptionAccessor and not returning null when calling ExceptionAccessor.getException().
    • toRootCause

      public static Throwable toRootCause(Throwable aThrowable)
      Traverses through the Throwable's cause instances recursively until the root cause is detected (this is the cause which has no cause any more).
      Parameters:
      aThrowable - The Throwable for which to get the root cause.
      Returns:
      The root cause of the provided Throwable instance or the instance itself if it has no cause.
    • isThrownAsOfAlreadyClosed

      public static boolean isThrownAsOfAlreadyClosed(IOException aException)
      Tests whether the provided exception is thrown as of something already being closed. In some cases, when something is already closed and some (wrapper) method tries to close it (again), then no exception should be propagated. This is very implementation specific regarding the underlying JDK as we expect something like "Pipe closed", "Connection closed" or the like to be contained in the message.
      Parameters:
      aException - The IOException to be tested.
      Returns:
      True in case we have an exception as of something to be closed was already caused, else false.