Package uk.ac.ebi.utils.exceptions
Class ExceptionUtils
java.lang.Object
uk.ac.ebi.utils.exceptions.ExceptionUtils
Utilities related to exception handling.
- date
- 2 Oct 2013
- Author:
- Marco Brandizi
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E extends Throwable>
EWrapper that usescause.getClass()
as exception type.static <E extends Throwable>
EWrapper with no cause.static <E extends Throwable>
EHelper to ease the building of an exception and its message.static Throwable
Gets the root cause of an exception.static Throwable
Goes through theexception's hierarchy
until it finds one that has a non-nullmessage
.static String
Takes the result thatgetSignificantException(Throwable)
returns and then returns itsThrowable.getMessage()
.static <E extends Throwable>
voidWrapper that usescause.getClass()
as exception type.static <E extends Throwable>
voidA wrapper with no cause.static <E extends Throwable>
voidThis callsbuildEx(Class, Throwable, String, Object...)
and then throws the built exception.
-
Method Details
-
getRootCause
Gets the root cause of an exception. To do that, it traces back the hierarchy of the exception'scauses
, until it finds an exception that has no upper cause. Eventually, it returns the first exception that has no cause attached and hence it always returns a non-null result.- Throws:
NullPointerException
- if ex is null.
-
getSignificantException
Goes through theexception's hierarchy
until it finds one that has a non-nullmessage
. This might return the initial exception itself. If none of the exceptions in the exception hierarchy has a non-null message, the method returns null.- Throws:
NullPointerException
- if ex is null.
-
getSignificantMessage
Takes the result thatgetSignificantException(Throwable)
returns and then returns itsThrowable.getMessage()
. Returns null if the significant exception is null. -
buildEx
public static <E extends Throwable> E buildEx(Class<E> exType, Throwable cause, String messageTemplate, Object... params) Helper to ease the building of an exception and its message. Builds an exception instance of exType, with the given message template instantiated with the given parameters. The message template is passed toString.format(String, Object...)
, so you have to use the printf-style rules (eg, '%s' for a string parameter). Additionally, you can use$cause
or ${cause} to have thecause's message
reported. The new exception is assigned a cause, if the corresponding parameter is non-null. -
buildEx
public static <E extends Throwable> E buildEx(Class<E> exType, String messageTemplate, Object... params) Wrapper with no cause. -
buildEx
public static <E extends Throwable> E buildEx(E cause, String messageTemplate, Object... params) throws E Wrapper that usescause.getClass()
as exception type.- Throws:
E extends Throwable
-
throwEx
public static <E extends Throwable> void throwEx(Class<E> exType, Throwable cause, String messageTemplate, Object... params) throws E This callsbuildEx(Class, Throwable, String, Object...)
and then throws the built exception. Note that Java will consider this method as throwing checked/unchecked exception code, depending on the type of E. Note that you cannot always use this to wrap the body of a function, since, in this case Java will not know that you're certainly throwing an exception and hence it will think that you are not returning any value after the catch clause that uses this method. If you are in this situation, usethrow buildEx (...)
instead.- Throws:
E extends Throwable
-
throwEx
public static <E extends Throwable> void throwEx(Class<E> exType, String messageTemplate, Object... params) throws E A wrapper with no cause.- Throws:
E extends Throwable
-
throwEx
public static <E extends Throwable> void throwEx(E cause, String messageTemplate, Object... params) throws E Wrapper that usescause.getClass()
as exception type.- Throws:
E extends Throwable
-