Class Unchecked


  • public final class Unchecked
    extends java.lang.Object
    Static utility methods that convert checked exceptions to unchecked.

    Two wrap() methods are provided that can wrap an arbitrary piece of logic and convert checked exceptions to unchecked.

    A number of other methods are provided that allow a lambda block to be decorated to avoid handling checked exceptions. For example, the method File.getCanonicalFile() throws an IOException which can be handled as follows:

      stream.map(Unchecked.function(file -> file.getCanonicalFile())
     

    Each method accepts a functional interface that is defined to throw Throwable. Catching Throwable means that any method can be wrapped. Any InvocationTargetException is extracted and processed recursively. Any IOException is converted to an UncheckedIOException. Any ReflectiveOperationException is converted to an UncheckedReflectiveOperationException. Any Error or RuntimeException is re-thrown without alteration. Any other exception is wrapped in a RuntimeException.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T,​U>
      java.util.function.BiConsumer<T,​U>
      biConsumer​(CheckedBiConsumer<T,​U> consumer)
      Converts checked exceptions to unchecked based on the BiConsumer interface.
      static <T,​U,​R>
      java.util.function.BiFunction<T,​U,​R>
      biFunction​(CheckedBiFunction<T,​U,​R> function)
      Converts checked exceptions to unchecked based on the BiFunction interface.
      static <T> java.util.function.BinaryOperator<T> binaryOperator​(CheckedBinaryOperator<T> function)
      Converts checked exceptions to unchecked based on the BinaryOperator interface.
      static <T,​U>
      java.util.function.BiPredicate<T,​U>
      biPredicate​(CheckedBiPredicate<T,​U> predicate)
      Converts checked exceptions to unchecked based on the BiPredicate interface.
      static <T> java.util.function.Consumer<T> consumer​(CheckedConsumer<T> consumer)
      Converts checked exceptions to unchecked based on the Consumer interface.
      static <T,​R>
      java.util.function.Function<T,​R>
      function​(CheckedFunction<T,​R> function)
      Converts checked exceptions to unchecked based on the Function interface.
      static <T> java.util.function.Predicate<T> predicate​(CheckedPredicate<T> predicate)
      Converts checked exceptions to unchecked based on the Predicate interface.
      static java.lang.RuntimeException propagate​(java.lang.Throwable throwable)
      Propagates throwable as-is if possible, or by wrapping in a RuntimeException if not.
      static java.lang.Runnable runnable​(CheckedRunnable runnable)
      Converts checked exceptions to unchecked based on the Runnable interface.
      static <R> java.util.function.Supplier<R> supplier​(CheckedSupplier<R> supplier)
      Converts checked exceptions to unchecked based on the Supplier interface.
      static <T> java.util.function.UnaryOperator<T> unaryOperator​(CheckedUnaryOperator<T> function)
      Converts checked exceptions to unchecked based on the UnaryOperator interface.
      static void wrap​(CheckedRunnable block)
      Wraps a block of code, converting checked exceptions to unchecked.
      static <T> T wrap​(CheckedSupplier<T> block)
      Wraps a block of code, converting checked exceptions to unchecked.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • wrap

        public static void wrap​(CheckedRunnable block)
        Wraps a block of code, converting checked exceptions to unchecked.
           Unchecked.wrap(() -> {
             // any code that throws a checked exception
           }
         

        If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Parameters:
        block - the code block to wrap
        Throws:
        java.io.UncheckedIOException - if an IO exception occurs
        java.lang.RuntimeException - if an exception occurs
      • wrap

        public static <T> T wrap​(CheckedSupplier<T> block)
        Wraps a block of code, converting checked exceptions to unchecked.
           Unchecked.wrap(() -> {
             // any code that throws a checked exception
           }
         

        If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the type of the result
        Parameters:
        block - the code block to wrap
        Returns:
        the result of invoking the block
        Throws:
        java.io.UncheckedIOException - if an IO exception occurs
        java.lang.RuntimeException - if an exception occurs
      • runnable

        public static java.lang.Runnable runnable​(CheckedRunnable runnable)
        Converts checked exceptions to unchecked based on the Runnable interface.

        This wraps the specified runnable returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Parameters:
        runnable - the runnable to be decorated
        Returns:
        the runnable instance that handles checked exceptions
      • function

        public static <T,​R> java.util.function.Function<T,​R> function​(CheckedFunction<T,​R> function)
        Converts checked exceptions to unchecked based on the Function interface.

        This wraps the specified function returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the input type of the function
        R - the return type of the function
        Parameters:
        function - the function to be decorated
        Returns:
        the function instance that handles checked exceptions
      • biFunction

        public static <T,​U,​R> java.util.function.BiFunction<T,​U,​R> biFunction​(CheckedBiFunction<T,​U,​R> function)
        Converts checked exceptions to unchecked based on the BiFunction interface.

        This wraps the specified function returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the first input type of the function
        U - the second input type of the function
        R - the return type of the function
        Parameters:
        function - the function to be decorated
        Returns:
        the function instance that handles checked exceptions
      • unaryOperator

        public static <T> java.util.function.UnaryOperator<T> unaryOperator​(CheckedUnaryOperator<T> function)
        Converts checked exceptions to unchecked based on the UnaryOperator interface.

        This wraps the specified operator returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the type of the operator
        Parameters:
        function - the function to be decorated
        Returns:
        the function instance that handles checked exceptions
      • binaryOperator

        public static <T> java.util.function.BinaryOperator<T> binaryOperator​(CheckedBinaryOperator<T> function)
        Converts checked exceptions to unchecked based on the BinaryOperator interface.

        This wraps the specified operator returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the type of the operator
        Parameters:
        function - the function to be decorated
        Returns:
        the function instance that handles checked exceptions
      • predicate

        public static <T> java.util.function.Predicate<T> predicate​(CheckedPredicate<T> predicate)
        Converts checked exceptions to unchecked based on the Predicate interface.

        This wraps the specified predicate returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the type of the predicate
        Parameters:
        predicate - the predicate to be decorated
        Returns:
        the predicate instance that handles checked exceptions
      • biPredicate

        public static <T,​U> java.util.function.BiPredicate<T,​U> biPredicate​(CheckedBiPredicate<T,​U> predicate)
        Converts checked exceptions to unchecked based on the BiPredicate interface.

        This wraps the specified predicate returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the first type of the predicate
        U - the second type of the predicate
        Parameters:
        predicate - the predicate to be decorated
        Returns:
        the predicate instance that handles checked exceptions
      • consumer

        public static <T> java.util.function.Consumer<T> consumer​(CheckedConsumer<T> consumer)
        Converts checked exceptions to unchecked based on the Consumer interface.

        This wraps the specified consumer returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the type of the consumer
        Parameters:
        consumer - the consumer to be decorated
        Returns:
        the consumer instance that handles checked exceptions
      • biConsumer

        public static <T,​U> java.util.function.BiConsumer<T,​U> biConsumer​(CheckedBiConsumer<T,​U> consumer)
        Converts checked exceptions to unchecked based on the BiConsumer interface.

        This wraps the specified consumer returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        T - the first type of the consumer
        U - the second type of the consumer
        Parameters:
        consumer - the consumer to be decorated
        Returns:
        the consumer instance that handles checked exceptions
      • supplier

        public static <R> java.util.function.Supplier<R> supplier​(CheckedSupplier<R> supplier)
        Converts checked exceptions to unchecked based on the Supplier interface.

        This wraps the specified supplier returning an instance that handles checked exceptions. If a checked exception is thrown it is converted to an UncheckedIOException or RuntimeException as appropriate.

        Type Parameters:
        R - the result type of the supplier
        Parameters:
        supplier - the supplier to be decorated
        Returns:
        the supplier instance that handles checked exceptions
      • propagate

        public static java.lang.RuntimeException propagate​(java.lang.Throwable throwable)
        Propagates throwable as-is if possible, or by wrapping in a RuntimeException if not.
        • If throwable is an InvocationTargetException the cause is extracted and processed recursively.
        • If throwable is an Error or RuntimeException it is propagated as-is.
        • If throwable is an IOException it is wrapped in UncheckedIOException and thrown.
        • If throwable is an ReflectiveOperationException it is wrapped in UncheckedReflectiveOperationException and thrown.
        • Otherwise throwable is wrapped in a RuntimeException and thrown.
        This method always throws an exception. The return type is a convenience to satisfy the type system when the enclosing method returns a value. For example:
           T foo() {
             try {
               return methodWithCheckedException();
             } catch (Exception e) {
               throw Unchecked.propagate(e);
             }
           }
         
        Parameters:
        throwable - the Throwable to propagate
        Returns:
        nothing; this method always throws an exception