Skip to content
Advertisement

Are all exceptions throw from Jdbi of type JdbiException?

It states here:

https://jdbi.org/apidocs/org/jdbi/v3/core/JdbiException.html

that JdbiException is the Base unchecked exception for exceptions thrown from jdbi.

However, if I’m calling the withHandle method with various different callbacks:

jdbi.withHandle(handle -> handle
            .createQuery("...")
            .mapTo(String.class)
            .one());  

the docs state that it throws X extends Exception (rather than throwing JdbiExecption as I would have expected) and describes it as @param <X> exception type thrown by the callback, if any.:

public <R, X extends Exception> R withHandle(HandleCallback<R, X> callback) throws X {

I want to know if it is safe to do call withHandle and just catch JdbiException, rather than having to catch Exception?

try {
  jdbi.withHandle(handle -> ...);
} catch (JdbiException e) {
    // Will this catch everything thrown from `withHandle`?
}

Advertisement

Answer

The point of that X extends Exception is for your code, not JDBI’s code. The code you write yourself (after the ->) can throw X.

JDBI will indeed be throwing JdbiExceptions, and won’t be throwing anything else. But YOUR CODE might e.g. throw IOException or whatnot.

This works:

try {
  jdbi.withHandle(handle -> throw new IOException());
} catch (IOException e) {}

and to make that work, that’s what the <X extends Exception> is all about.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement