Package libcore.util
Class SneakyThrow
java.lang.Object
libcore.util.SneakyThrow
public final class SneakyThrow extends Object
Exploits a weakness in the runtime to throw an arbitrary throwable without
the traditional declaration. This is a dangerous API that should be
used with great caution. Typically this is useful when rethrowing
throwables that are of a known range of types.
The following code must enumerate several types to rethrow:
public void close() throws IOException {
Throwable thrown = null;
...
if (thrown != null) {
if (thrown instanceof IOException) {
throw (IOException) thrown;
} else if (thrown instanceof RuntimeException) {
throw (RuntimeException) thrown;
} else if (thrown instanceof Error) {
throw (Error) thrown;
} else {
throw new AssertionError();
}
}
}
With SneakyThrow, rethrowing is easier:
public void close() throws IOException {
Throwable thrown = null;
...
if (thrown != null) {
SneakyThrow.sneakyThrow(thrown);
}
}-
Method Summary
Modifier and Type Method Description static voidsneakyThrow(Throwable t)
-
Method Details
-
sneakyThrow
-