Class ExceptionUtils

java.lang.Object
uk.ac.ebi.utils.exceptions.ExceptionUtils

public class ExceptionUtils extends Object
Utilities related to exception handling.
date
2 Oct 2013
Author:
Marco Brandizi
  • Method Details

    • getRootCause

      public static Throwable getRootCause(Throwable ex)
      Gets the root cause of an exception. To do that, it traces back the hierarchy of the exception's causes, 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

      public static Throwable getSignificantException(Throwable ex)
      Goes through the exception's hierarchy until it finds one that has a non-null message. 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

      public static String getSignificantMessage(Throwable ex)
      Takes the result that getSignificantException(Throwable) returns and then returns its Throwable.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 to String.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 the cause'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 uses cause.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 calls buildEx(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, use throw 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 uses cause.getClass() as exception type.
      Throws:
      E extends Throwable