0

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`?
}

rmf
  • 625
  • 2
  • 9
  • 39

1 Answers1

3

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks, so if *my code* is always code that is calling some JDBI method, such `handle -> handle().createQuery(...).mapTo(...).one()` or `handle -> handle.createUpdate(...).execute()`, then I can assume that only JdbiExceptions will be thrown? – rmf Sep 17 '21 at 13:53
  • Yup. you got it :) – rzwitserloot Sep 17 '21 at 14:56