Enum Rethrower

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Rethrower>

    public enum Rethrower
    extends java.lang.Enum<Rethrower>
    Rethrowing checked exceptions as unchecked ones. Eh, it is sometimes useful...
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Rethrower.Procedure  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void rethrowIfUnrecoverable​(java.lang.Throwable exception)
      Rethrow the supplied exception if it is unrecoverable.
      static void swallow​(Rethrower.Procedure procedure)  
      static <T extends java.lang.Throwable>
      void
      throwAs​(java.lang.Throwable t)
      Rethrows t (identical object).
      static java.lang.RuntimeException throwAsUncheckedException​(java.lang.Throwable t)
      Throw the supplied Throwable, masked as an unchecked exception.
      static void toIllegalArgument​(Rethrower.Procedure voidCallable)  
      static void toIllegalState​(Rethrower.Procedure voidCallable)  
      static void toRuntime​(Rethrower.Procedure voidCallable)
      Catch a checked exception and rethrow as a RuntimeException.
      static <T> T toRuntime​(java.util.concurrent.Callable<T> callable)
      Catch a checked exception and rethrow as a RuntimeException
      static Rethrower valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static Rethrower[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • values

        public static Rethrower[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (Rethrower c : Rethrower.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static Rethrower valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • rethrowIfUnrecoverable

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

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

      • throwAsUncheckedException

        public static java.lang.RuntimeException throwAsUncheckedException​(java.lang.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
      • throwAs

        public static <T extends java.lang.Throwable> void throwAs​(java.lang.Throwable t)
                                                            throws T extends java.lang.Throwable
        Rethrows t (identical object).
        Throws:
        T extends java.lang.Throwable
      • toRuntime

        public static <T> T toRuntime​(java.util.concurrent.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)