Interface ThrowingFunction<T,R>

Type Parameters:
T - type of the input element
R - type of the result element
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ThrowingFunction<T,R>
This functional interface allows to not catch an Exception in a lambda expression : use it at your own risk, the exception becomes unchecked. It is recommended to use it only in a method that can throw the Exception type that is unchecked. So the exception can be checked when using the method. Source : http://4comprehension.com/sneakily-throwing-exceptions-in-lambda-expressions-in-java/
Author:
Guillaume Pérution-Kihli
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(T t)
    Applies this function.
    static <T extends Exception, R>
    R
    Rethrows the given exception without declaring it.
    static <T,R> Function<T,R>
    Wraps a throwing function into a standard unchecked function.
  • Method Details

    • apply

      R apply(T t) throws Exception
      Applies this function.
      Parameters:
      t - the function input
      Returns:
      the function result
      Throws:
      Exception - if evaluation fails
    • sneakyThrow

      static <T extends Exception, R> R sneakyThrow(Exception t) throws T
      Rethrows the given exception without declaring it.
      Type Parameters:
      T - checked exception type used for the cast
      R - dummy return type
      Parameters:
      t - the exception to throw
      Returns:
      never returns normally
      Throws:
      T - always thrown
    • unchecked

      static <T,R> Function<T,R> unchecked(ThrowingFunction<T,R> f)
      Wraps a throwing function into a standard unchecked function.
      Type Parameters:
      T - input type
      R - result type
      Parameters:
      f - the function to wrap
      Returns:
      a standard function rethrowing checked exceptions unchecked