Java SneakyThrow of exceptions, type erasure
Can someone explain this code? public class SneakyThrow { public static void sneakyThrow(Throwable ex) { SneakyThrow.<RuntimeException>sneakyThrowInner(ex); } private static <T extends Throwable> T sneakyThrowInner(Throwable ex) throws T { throw (T) ex; } public static void main(String[] args) { SneakyThrow.sneakyThrow(new Exception()); } } It may seems strange, but this doesn't produce a cast exception, and permits to throw a checked exception without having to declare it in the signature, or to wrap it in an unchecked exception. Notice that neither sneakyThrow(...) or the main are declaring