Thursday, October 3, 2024
HomeProgrammingUse jOOλ’s Sneaky Throw to Keep away from Checked Exceptions – Java,...

Use jOOλ’s Sneaky Throw to Keep away from Checked Exceptions – Java, SQL and jOOQ.


Don’t you hate how you must wrap checked exception throwing code in static initialisers? E.g. you can not write this in Java:

public class Check {
    static remaining Class<?> klass = Class.forName("org.h2.Driver");
}

There’s an unhandled ClassNotFoundException, and you’ll’t catch / rethrow it merely. A static initialiser is required:

public class Check {
    static remaining Class<?> klass;

    static {
        strive {
            klass = Class.forName("org.h2.Driver");
        }
        catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

Yuck.

Fortunately, one in all jOOλ’s lesser recognized options is the Sneaky class, which accommodates a bunch of utility strategies that wrap a JDK purposeful interface to an equal, “sneaky-throwing” purposeful interface that doesn’t declare the checked exception.

Briefly, you’ll be able to write:

public class Check {
    static remaining Class<?> klass = Sneaky.provider(
        () -> Class.forName("org.h2.Driver")
    ).get();
}

The exception is solely re-thrown “sneakily”, because the JVM doesn’t care about an exception’s checked-ness. Should you don’t have H2 in your classpath, you’ll get:

Exception in thread "most important" java.lang.ExceptionInInitializerError
Brought on by: java.lang.ClassNotFoundException: org.h2.Driver
	at java.base/jdk.inside.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.inside.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at java.base/java.lang.Class.forName0(Native Methodology)
	at java.base/java.lang.Class.forName(Class.java:375)
	at org.jooq.take a look at.util.Check.lambda$0(Check.java:44)
	at org.jooq.lambda.Unchecked.lambda$provider$38(Unchecked.java:1695)
	at org.jooq.take a look at.util.Check.<clinit>(Check.java:44)

You should utilize this method wherever else a JDK purposeful interface is required, and also you don’t care about an exception’s checked-ness, e.g. in streams:

// Does not compile:
Stream
    .generate(
        () -> Class.forName("org.h2.Driver"))
    .restrict(1)
    .forEach(System.out::println);

// So ugly
Stream
    .generate(
        () -> {
            strive {
                return Class.forName("org.h2.Driver");
            }
            catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        })
    .restrict(1)
    .forEach(System.out::println);

// Use jOOλ's Sneaky provider
Stream
    .generate(Sneaky.provider(
        () -> Class.forName("org.h2.Driver")))
    .restrict(1)
    .forEach(System.out::println);

Get jOOλ right here: https://github.com/jOOQ/jOOL

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments