Class CheckedExceptionHandler


  • public abstract class CheckedExceptionHandler
    extends Object
    Represents downgrading policy for checked exceptions. The exception policy is akin to a reusable catch block that catches checked exception and throws an unchecked one. Method handle(Exception) defines downgrading mechanism, typically by wrapping the checked exception in an unchecked one, but there are special cases like Exceptions.sneak(), which downgrade only method signature without altering the exception itself. Methods of this class apply the exception policy to functional interfaces (usually lambdas) by wrapping them in a try-catch block. See noexception tutorial.

    Typical usage: Exceptions.sneak().get(() -> my_throwing_lambda)

    CheckedExceptionHandler does not stop propagation of any exceptions (checked or unchecked). ExceptionHandler is used for that purpose. The two classes can be used together by first downgrading checked exceptions with CheckedExceptionHandler and then applying exception handling policy with ExceptionHandler.

    Combined usage: Exceptions.silence().get(Exceptions.sneak().supplier(() -> my_throwing_lambda)).orElse(fallback)

    All wrapping methods surround the functional interface with a try-catch block. Only checked exceptions are handled. Unchecked exceptions are propagated to caller. If the functional interface throws checked exception, the exception is caught and passed to handle(Exception), which converts it to an unchecked exception, which is then thrown.

    Wrapping methods for all standard functional interfaces are provided. Simple interfaces have short method names, like runnable(ThrowingRunnable) or supplier(ThrowingSupplier). Interfaces with longer names have methods that follow fromX naming pattern, for example fromUnaryOperator(ThrowingUnaryOperator). Parameterless functional interfaces can be called directly by methods run(ThrowingRunnable), get(ThrowingSupplier), and the various getAsX variants. All methods take throwing versions of standard functional interfaces, for example ThrowingRunnable or ThrowingSupplier.

    See Also:
    Tutorial, handle(Exception), Exceptions, ExceptionHandler, ExceptionFilter