Saturday, June 4, 2022
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 need to wrap checked exception throwing code in static initialisers? E.g. you can’t write this in Java:

public class Take a look at {
    static ultimate 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 Take a look at {
    static ultimate Class<?> klass;

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

Yuck.

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

In brief, you’ll be able to write:

public class Take a look at {
    static ultimate 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 "essential" 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 Technique)
	at java.base/java.lang.Class.forName(Class.java:375)
	at org.jooq.check.util.Take a look at.lambda$0(Take a look at.java:44)
	at org.jooq.lambda.Unchecked.lambda$provider$38(Unchecked.java:1695)
	at org.jooq.check.util.Take a look at.<clinit>(Take a look at.java:44)

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

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

// So ugly
Stream
    .generate(
        () -> {
            attempt {
                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

- Advertisment -
Google search engine

Most Popular

Recent Comments