java - proper way to capture meaningful sql exception -


in java try - catch clause, want able capture exact cause of sql exception (i using postgres, question applies jdbc drivers). had is

} catch (throwable ex) {         // rollback         ex.printstacktrace();         string message = ex.getmessage();         throwable cause = ex.getcause();         if (cause instanceof constraintviolationexception){             sqlexception exc = ((constraintviolationexception)cause).getsqlexception();             if (exc instanceof batchupdateexception){                 sqlexception nextex = ((batchupdateexception)exc).getnextexception();                 if (nextex instanceof psqlexception){                     message = ((psqlexception)nextex).getmessage();                 }             }         }         throw new recordserviceexception(message);      } 

this awful, , works specific type of exception. isn't there more valid way capture meaningful sql exception?

from oracle docs: http://docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html

public static void printsqlexception(sqlexception ex) {      (throwable e : ex) {         if (e instanceof sqlexception) {             if (ignoresqlexception(                 ((sqlexception)e).                 getsqlstate()) == false) {                  e.printstacktrace(system.err);                 system.err.println("sqlstate: " +                     ((sqlexception)e).getsqlstate());                  system.err.println("error code: " +                     ((sqlexception)e).geterrorcode());                  system.err.println("message: " + e.getmessage());                  throwable t = ex.getcause();                 while(t != null) {                     system.out.println("cause: " + t);                     t = t.getcause();                 }             }         }     } } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -