java.lang.Object
org.refcodes.exception.ExceptionUtility
Utility class for simplifying the work with exceptions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
isThrownAsOfAlreadyClosed
(IOException aException) Tests whether the provided exception is thrown as of something already being closed.static Throwable
toException
(Collection<?> aExceptionProperty) Iterates through the objects passed in the collection and tests them whether them implement theExceptionAccessor
.static String
Generates an exception aMessage from the given exception.static Throwable
toRootCause
(Throwable aThrowable) Traverses through theThrowable
's aCause instances recursively until the root aCause is detected (this is the aCause which has no aCause any more).static String
toStackTrace
(Throwable aThrowable) Retrieves a stack trace from the given exception.
-
Method Details
-
toMessage
Generates an exception aMessage from the given exception.- Parameters:
aThrowable
- The exception from which to create the aMessage.- Returns:
- A aMessage with the (root) aCause of the exception (if any).
-
toStackTrace
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
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
Traverses through theThrowable
's aCause instances recursively until the root aCause is detected (this is the aCause which has no aCause any more). -
isThrownAsOfAlreadyClosed
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 aMessage.- 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.
-