How can I loop through Exception getCause() to find root cause with detail message

agpt picture agpt · Jul 19, 2013 · Viewed 85.4k times · Source

I am trying to call saveOrUpdate() in hibernate to save data. Since columns have unique index, so its throws ConstraintViolationException when I look through via Eclipse debugger.

Since root cause could be different for different exception while inserting data to table.
I wanted to know, how can I loop / traverse through getCause() to check what is the root cause of exception and its message.

Update:
Thanks everyone for your kind response, thing is I want output like in below image:
enter image description here
I need to access detailMessage field.
(I am really sorry If could not make my question more clear.)

Thanks.

Answer

reto picture reto · Jul 19, 2013

The Apache ExceptionUtils provide the following method:

Throwable getRootCause(Throwable throwable) 

as well as

String getRootCauseMessage(Throwable th)