Package cn.sliew.milky.common.exception
Class Rethrower
- java.lang.Object
-
- cn.sliew.milky.common.exception.Rethrower
-
public final class Rethrower extends Object
Rethrowing checked exceptions as unchecked ones. Eh, it is sometimes useful...
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceRethrower.Procedure
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidrethrowIfUnrecoverable(Throwable exception)Rethrow the suppliedexceptionif it is unrecoverable.static voidswallow(Rethrower.Procedure procedure)static <T extends Throwable>
voidthrowAs(Throwable t)Rethrowst(identical object).static RuntimeExceptionthrowAsUncheckedException(Throwable t)Throw the suppliedThrowable, masked as an unchecked exception.static voidtoIllegalArgument(Rethrower.Procedure voidCallable)static voidtoIllegalState(Rethrower.Procedure voidCallable)static voidtoRuntime(Rethrower.Procedure voidCallable)Catch a checked exception and rethrow as aRuntimeException.static <T> TtoRuntime(Callable<T> callable)Catch a checked exception and rethrow as aRuntimeException
-
-
-
Method Detail
-
rethrowIfUnrecoverable
public static void rethrowIfUnrecoverable(Throwable exception)
Rethrow the suppliedexceptionif it is unrecoverable.If the supplied
exceptionis not unrecoverable, this method does nothing.
-
throwAsUncheckedException
public static RuntimeException throwAsUncheckedException(Throwable t)
Throw the suppliedThrowable, masked as an unchecked exception.The supplied
Throwablewill 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- theThrowableto throw as an unchecked exception; nevernull- 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
throwstatement
-
throwAs
public static <T extends Throwable> void throwAs(Throwable t) throws T extends Throwable
Rethrowst(identical object).- Throws:
T extends Throwable
-
toRuntime
public static <T> T toRuntime(Callable<T> callable)
Catch a checked exception and rethrow as aRuntimeException- 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 aRuntimeException.- Parameters:
voidCallable- function that throws a checked exception.
-
toIllegalState
public static void toIllegalState(Rethrower.Procedure voidCallable)
-
toIllegalArgument
public static void toIllegalArgument(Rethrower.Procedure voidCallable)
-
swallow
public static void swallow(Rethrower.Procedure procedure)
-
-