Class ExecutionExceptions

    • Method Detail

      • wrapAndThrow

        public static <X extends Throwable> void wrapAndThrow​(ExecutionException ee,
                                                              Class<? extends X> xClass,
                                                              BiFunction<? super String,​? super ExecutionException,​? extends X> xSupplier)
                                                       throws X extends Throwable
        Wraps and throws an ExecutionException when its cause is an instance of xClass. This is compatible with Lambda method references on common throwable constructors that take (String message, Throwable cause).

        First, an attempt is made to create a surrogate of the cause via Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable), with the execution exception being the cause of the new surrogate. When a surrogate cannot be created, uses the provided function xSupplier to create a new wrapper.

        When an ExecutionException occurs, unwrapping the cause may lose important stack trace information, since the cause is likely processed on a different thread and will not have the full caller stack trace.

        Furthermore, it is desirable to be able to maintain expected exception types. This wrapping will help maintain exception types while not losing critical stack trace information.

        This is expected to typically used within a catch block, to maintain exception types:

        try {
           …
           return future.get();
         } catch(ExecutionException ee) {
           wrapAndThrow(ee, IOException.class, IOException::new);
           throw ee;
         }
        Parameters:
        xClass - Exceptions with causes of this class are wrapped and thrown.
        xSupplier - Performs wrapping of the execution exception itself when a surrogate cannot be created.
        Throws:
        X - When cause is an instance of xClass, throws ee wrapped via xSupplier.
        X extends Throwable
        See Also:
        Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable)
      • wrapAndThrowWithTemplate

        public static <X extends Throwable> void wrapAndThrowWithTemplate​(ExecutionException ee,
                                                                          Class<? extends X> xClass,
                                                                          BiFunction<? super X,​? super ExecutionException,​? extends X> xSupplier)
                                                                   throws X extends Throwable
        Wraps and throws an ExecutionException when its cause is an instance of xClass.

        First, an attempt is made to create a surrogate of the cause via Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable), with the execution exception being the cause of the new surrogate. When a surrogate cannot be created, uses the provided function xSupplier to create a new wrapper.

        When an ExecutionException occurs, unwrapping the cause may lose important stack trace information, since the cause is likely processed on a different thread and will not have the full caller stack trace.

        Furthermore, it is desirable to be able to maintain expected exception types. This wrapping will help maintain exception types while not losing critical stack trace information.

        This is expected to typically used within a catch block, to maintain exception types:

        try {
           …
           return future.get();
         } catch(ExecutionException ee) {
           wrapAndThrowWithCause(ee, SQLException.class, (template, cause)
             -> new SQLException(template.getMessage(), template.getSQLState(), template.getErrorCode(), cause)));
           throw ee;
         }
        Parameters:
        xClass - Exceptions with causes of this class are wrapped and thrown.
        xSupplier - Performs wrapping of the execution exception itself when a surrogate cannot be created.
        Throws:
        X - When cause is an instance of xClass, throws ee wrapped via xSupplier.
        X extends Throwable
        See Also:
        Throwables.newSurrogate(java.lang.Throwable, java.lang.Throwable)