Class Rethrower


  • public final class Rethrower
    extends Object
    Rethrowing checked exceptions as unchecked ones. Eh, it is sometimes useful...
    • Method Detail

      • rethrowIfUnrecoverable

        public static void rethrowIfUnrecoverable​(Throwable exception)
        Rethrow the supplied exception if it is unrecoverable.

        If the supplied exception is not unrecoverable, this method does nothing.

      • throwAsUncheckedException

        public static RuntimeException throwAsUncheckedException​(Throwable t)
        Throw the supplied Throwable, masked as an unchecked exception.

        The supplied Throwable will not be wrapped. Rather, it will be thrown as is using an exploit of the Java language that relies on a combination of generics and type erasure to trick the Java compiler into believing that the thrown exception is an unchecked exception even if it is a checked exception.

        Warning

        This method should be used sparingly.

        Parameters:
        t - the Throwable to throw as an unchecked exception; never null
        Returns:
        this method always throws an exception and therefore never returns anything; the return type is merely present to allow this method to be supplied as the operand in a throw statement
      • toRuntime

        public static <T> T toRuntime​(Callable<T> callable)
        Catch a checked exception and rethrow as a RuntimeException
        Type Parameters:
        T - return type of the function.
        Parameters:
        callable - function that throws a checked exception.
        Returns:
        object that the function returns.
      • toRuntime

        public static void toRuntime​(Rethrower.Procedure voidCallable)
        Catch a checked exception and rethrow as a RuntimeException.
        Parameters:
        voidCallable - function that throws a checked exception.
      • toIllegalArgument

        public static void toIllegalArgument​(Rethrower.Procedure voidCallable)