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)
    Redefine the apply method to be able to declare that an exception is thrown
    static <T extends Exception, R>
    R
    Sneakily rethrows the supplied exception as an unchecked one.
    static <T,R> Function<T,R>
    Wraps a throwing function into a standard unchecked function.
  • Method Details

    • apply

      R apply(T t) throws Exception
      Redefine the apply method to be able to declare that an exception is thrown
      Parameters:
      t - the element
      Returns:
      the result of the function
      Throws:
      Exception - if an exception is thrown by the inner method
    • sneakyThrow

      static <T extends Exception, R> R sneakyThrow(Exception t) throws T
      Sneakily rethrows the supplied exception as an unchecked one.
      Type Parameters:
      T - type of the exception to throw
      R - type of the result element
      Parameters:
      t - the exception
      Returns:
      nothing
      Throws:
      T - the initial exception
    • unchecked

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