Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12
This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.
Throw exceptions
Supported by ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
Some databases, such as SQL Server or Sybase, can raise several errors from a single statement or batch, for example when executing procedural logic that interleaves result sets, update counts, and errors. JDBC exposes these errors through Statement.getMoreResults() and chained java.sql.SQLException instances. The throwExceptions setting governs how jOOQ propagates them. The possible values of org.jooq.conf.ThrowExceptions are:
-
THROW_ALL(the default): All exceptions are consumed, chained usingSQLException.setNextException(), and thrown together, wrapped in aorg.jooq.exception.DataAccessException. -
THROW_FIRST: Only the first exception is thrown. Any further exceptions are not consumed. -
THROW_NONE: No exception is thrown. When fetching many results, the exceptions are collected in the resultingorg.jooq.Resultsinstead, alongside the results and update counts.
With THROW_NONE, each exception is available from its org.jooq.ResultOrRows element:
Results results = DSL.using(connection, dialect, new Settings().withThrowExceptions(THROW_NONE))
.fetchMany("...");
for (ResultOrRows r : results.resultsOrRows()) {
if (r.exception() != null)
System.out.println("Exception: " + r.exception().getMessage());
else if (r.result() != null)
System.out.println("Result:\n" + r.result());
else
System.out.println("Update count: " + r.rows());
}
Example configuration
Settings settings = new Settings()
.withThrowExceptions(ThrowExceptions.THROW_FIRST); // Defaults to THROW_ALL
Feedback
Do you have any feedback about this page? We'd love to hear it!