| Modifier and Type | Method and Description |
|---|---|
static RuntimeException |
sneakyThrow(Throwable t)
The given
Throwable is sneakily thrown by this method. |
public static RuntimeException sneakyThrow(@Nonnull Throwable t)
Throwable is sneakily thrown by this method. This means that there is no need to catch it, nor
to declare that you throw it using the throws keyword. The exception is still thrown, but the Java compiler stops
warning about it. The following example demonstrates how to use this method correctly.
public void run() {
throw sneakyThrow(new IOException("Sneaky Thrown!"));
}
The given Throwable is not wrapped (f.e in a RuntimeException, ignored, swallowed, or redefined.
The JVM actually does not know or care about the concept of a 'checked exceptions'. All this method does is hide
the act of throwing a checked exception from the java compiler.
Note: that this method has a return type of RuntimeException. It is therefore advised you always call
this method as argument to the throw statement to avoid compiler errors regarding no return statement and
similar problems. This method won't, of course, return an actual RuntimeException. It never returns, it
always throws the provided exception.
Warning: This method should be save, but use with care.
t - The throwable to throw without requiring you to catch its type.NullPointerException - If given argument was nullCopyright © 2015-2016 Gridtec. All Rights Reserved.