Don't you hate how you have to wrap checked exception throwing code in static initialisers? E.g. you cannot write this in Java: public class Test { static final Class<?> klass = Class.forName("org.h2.Driver"); } There's an unhandled ClassNotFoundException, and you can't catch / rethrow it simply. A static initialiser is needed: public class Test { static … Continue reading Use jOOλ’s Sneaky Throw to Avoid Checked Exceptions
Tag: checked exceptions
Java 8 Friday: Better Exceptions
At Data Geekery, we love Java. And as we're really into jOOQ's fluent API and query DSL, we're absolutely thrilled about what Java 8 will bring to our ecosystem. Java 8 Friday Every Friday, we're showing you a couple of nice new tutorial-style Java 8 features, which take advantage of lambda expressions, extension methods, and … Continue reading Java 8 Friday: Better Exceptions
Throw checked exceptions like runtime exceptions in Java
How to throw a checked exception without catch block or throws clause in Java? Simple! public class Test { // No throws clause here public static void main(String[] args) { doThrow(new SQLException()); } static void doThrow(Exception e) { Test.<RuntimeException> doThrow0(e); } @SuppressWarnings("unchecked") static <E extends Exception> void doThrow0(Exception e) throws E { throw (E) e; … Continue reading Throw checked exceptions like runtime exceptions in Java