Class Exceptions


  • public final class Exceptions
    extends Object

    Collection of general utility methods with respect to working with exceptions. So far there's only an unwrapper and a type checker.

    Usage

    Some examples:

     // Check if the caught exception has a ConstraintViolationException in its hierarchy.
     catch (PersistenceException e) {
         if (Exceptions.is(e, ConstraintViolationException.class)) {
             // ...
         }
     }
     
     // Unwrap the caught FacesException until a non-FacesException is found.
     catch (FacesException e) {
         Exception realRootCause = Exceptions.unwrap(e, FacesException.class);
         // ...
     }
     
    Author:
    Bauke Scholtz
    • Method Detail

      • unwrap

        @SafeVarargs
        public static Throwable unwrap​(Throwable exception,
                                       Class<? extends Throwable>... types)
        Unwrap the nested causes of given exception as long as until it is not an instance of the given types and then return it. If the given exception is already not an instance of the given types, then it will directly be returned. Or if the exception, unwrapped or not, does not have a nested cause anymore, then it will be returned. This is particularly useful if you want to unwrap the real root cause out of a nested hierarchy of ServletException or FacesException.
        Parameters:
        exception - The exception to be unwrapped.
        types - The types which need to be unwrapped.
        Returns:
        The unwrapped root cause.
      • unwrap

        public static Throwable unwrap​(Throwable exception)
        Unwrap the nested causes of given exception as long as until it is not an instance of FacesException (Mojarra) or ELException (MyFaces) and then return it. If the given exception is already not an instance of the mentioned types, then it will directly be returned. Or if the exception, unwrapped or not, does not have a nested cause anymore, then it will be returned.
        Parameters:
        exception - The exception to be unwrapped from FacesException and ELException.
        Returns:
        The unwrapped root cause.
        Since:
        1.4
      • is

        public static <T extends Throwable> boolean is​(Throwable exception,
                                                       Class<T> type)
        Returns true if the given exception or one of its nested causes is an instance of the given type.
        Type Parameters:
        T - The generic throwable type.
        Parameters:
        exception - The exception to be checked.
        type - The type to be compared to.
        Returns:
        true if the given exception or one of its nested causes is an instance of the given type.
      • extract

        public static <T extends Throwable> T extract​(Throwable exception,
                                                      Class<T> type)
        Returns the first encountered exception of the given type while cascading into the given exception, or null if no such exception is found.
        Type Parameters:
        T - The generic throwable type.
        Parameters:
        exception - The exception to be checked.
        type - The type to be extracted.
        Returns:
        The first encountered exception of the given type while cascading into the given exception, or null if no such exception is found.
        Since:
        3.10