In addition, a single error can generate more than one SQLException. Use a loop and the getNextException method to process all SQLExceptions in the chain. In many cases, the second exception in the chain is the pertinent one.
The following is an example:
catch (Throwable e) {
System.out.println("exception thrown:");
errorPrint(e);
}
static void errorPrint(Throwable e) {
if (e instanceof SQLException)
SQLExceptionPrint((SQLException)e);
else
System.out.println("A non-SQL error: " + e.toString());
}
static void SQLExceptionPrint(SQLException sqle) {
while (sqle != null) {
System.out.println("\n---SQLException Caught---\n");
System.out.println("SQLState: " + (sqle).getSQLState());
System.out.println("Severity: " + (sqle).getErrorCode());
System.out.println("Message: " + (sqle).getMessage());
sqle.printStackTrace();
sqle = sqle.getNextException();
}
}
See also "Derby Exception Messages and SQL States", in the Derby Reference Manual.