Class LambdaContainerException

All Implemented Interfaces:
Serializable

@ClassVersion(sourceVersion="$Id: LambdaContainerException.java 1084 2024-01-03 15:31:20Z tquadrat $") @API(status=STABLE, since="0.1.0") public final class LambdaContainerException extends CheckedExceptionWrapper

A "container" exception for exception thrown within lambda expressions.

When a method that emits a checked exception is called inside a lambda function, this has to be caught inside that function; it is not possible to declare that exception for the method, as long as the underlying functional interface have not done that already.

Unfortunately, the methods from the interfaces in the package java.util.function do not declare any exception, for good reason. So the code below is not possible:

  …
  Appendable appendable = …
  Consumer appender = s -> appendable.append( s );
  appender.accept( "…" );
  …

because Appendable.append(CharSequence) declares to throw an IOException.

This class now is meant to wrap those exceptions and to allow them to bubble up to the caller:

  …
  Appendable appendable = …
  Consumer appender =
  {
    try
    {
      s -> appendable.append( s );
    }
    catch( IOException e )
    {
      throw new LambdaContainerException( e );
    }
  }

  try
  {
    appender.accept( "…" );
  }
  catch( LambdaContainerException e )
  {
    throw (IOException) e.getCause();
  }
  …

When said above that the methods from the functional interfaces in the java.util.function unfortunately do not declare any exception, this was only focused on their use with code that may emit checked exceptions. But in fact it is a good thing that the methods in these interfaces do not declare any exceptions, as this would have polluted any of the APIs that make use of these functional interfaces. And using the pattern above would be an alternative. Another would be the methods provided in the class Functions.

Author:
Thomas Thrien (thomas.thrien@tquadrat.org)
Version:
$Id: LambdaContainerException.java 1084 2024-01-03 15:31:20Z tquadrat $
Since:
0.1.0
See Also:
UML Diagram
UML Diagram for "org.tquadrat.foundation.exception.LambdaContainerException"

UML Diagram for "org.tquadrat.foundation.exception.LambdaContainerException"

UML Diagram for "org.tquadrat.foundation.exception.LambdaContainerException"
  • Constructor Details

    • LambdaContainerException

      Creates a new LambdaContainerException instance for the given exception.
      Parameters:
      e - The exception to wrap; cannot be null.
  • Method Details

    • checkIfExpected

      public final boolean checkIfExpected(Stream<Class<? extends Exception>> expected)
      Checks whether the contained Exception is somehow expected.
      Parameters:
      expected - The expected exceptions.
      Returns:
      true if the contained Exception is among the list of expected exceptions, false otherwise.
    • checkIfExpected

      public final boolean checkIfExpected(Collection<Class<? extends Exception>> expected)
      Checks whether the contained Exception is somehow expected.
      Parameters:
      expected - The expected exceptions.
      Returns:
      true if the contained Exception is among the list of expected exceptions, false otherwise.
    • checkIfExpected

      @SafeVarargs public final boolean checkIfExpected(Class<? extends Exception>... expected)
      Checks whether the contained Exception is somehow expected.
      Parameters:
      expected - The expected exceptions.
      Returns:
      true if the contained Exception is among the list of expected exceptions, false otherwise.
    • getCheckedCause

      public final <T> T getCheckedCause(Class<T> exceptionType) throws RuntimeException
      Returns the contained Exception if it is of the given type; otherwise a RuntimeException is thrown.
      Type Parameters:
      T - The expected Exception type.
      Parameters:
      exceptionType - The expected type.
      Returns:
      The contained Exception.
      Throws:
      RuntimeException - This is in fact an LambdaContainerException.UnexpectedException that is thrown when the contained Exception was not expected.