Example of processing SQLExceptions

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:");
static void errorPrint(Throwable e) {
    if (e instanceof SQLException) 
        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 = sqle.getNextException();

The SQLException may wrap another, triggering exception, like an IOException. To inspect this additional, wrapped error, call the SQLException's getCause method.

See also "Derby Exception Messages and SQL States", in the Java DB Reference Manual.