Package org.refcodes.exception
Class ExceptionUtility
- java.lang.Object
-
- org.refcodes.exception.ExceptionUtility
-
public final class ExceptionUtility extends java.lang.Object
Utility class for simplifying the work with exceptions.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
isThrownAsOfAlreadyClosed(java.io.IOException aException)
Tests whether the provided exception is thrown as of something already being closed.static java.lang.Throwable
toException(java.util.Collection<?> aExceptionProperty)
Iterates through the objects passed in the collection and tests them whether them implement theExceptionAccessor
.static java.lang.String
toMessage(java.lang.Throwable aThrowable)
Generates an exception message from the given exception.static java.lang.Throwable
toRootCause(java.lang.Throwable aThrowable)
Traverses through theThrowable
's cause instances recursively until the root cause is detected (this is the cause which has no cause any more).static java.lang.String
toStackTrace(java.lang.Throwable aThrowable)
Retrieves a stack trace from the given exception.
-
-
-
Method Detail
-
toMessage
public static java.lang.String toMessage(java.lang.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 java.lang.String toStackTrace(java.lang.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 java.lang.Throwable toException(java.util.Collection<?> aExceptionProperty)
Iterates through the objects passed in the collection and tests them whether them implement theExceptionAccessor
. The first one found implementing theExceptionAccessor
and returning an exception (instead of null), than this exception is returned.- Parameters:
aExceptionProperty
- The objects to test whether them implement theExceptionAccessor
.- Returns:
- The first exception retrieved from the first object implementing
the
ExceptionAccessor
and not returning null when callingExceptionAccessor.getException()
.
-
toRootCause
public static java.lang.Throwable toRootCause(java.lang.Throwable aThrowable)
Traverses through theThrowable
's cause instances recursively until the root cause is detected (this is the cause which has no cause any more).- Parameters:
aThrowable
- TheThrowable
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(java.io.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
- TheIOException
to be tested.- Returns:
- True in case we have an exception as of something to be closed was already caused, else false.
-
-