How to catch a ConstraintViolationException in JavaEE
The following piece of code catch a ConstraintValidationException in JavaEE so that we will be able to display an appropriate message to the...
https://www.czetsuyatech.com/2014/04/javaee-catch-constraintviolationexception.html
The following piece of code catch a ConstraintValidationException in JavaEE so that we will be able to display an appropriate message to the user instead of a long stack trace.
As you can see we catch EJBTransactionRolledbackException, why not ConstraintViolationException? Trust me it will not work because that exception is already wrapped by the container, so to be able to process it we need to unwrap it again.
try { //perform a transaction operation here, perhaps a delete } catch (EJBTransactionRolledbackException e) { Throwable t = e.getCause(); while ((t != null) && !(t instanceof ConstraintViolationException)) { t = t.getCause(); } if (t instanceof ConstraintViolationException) {} }
As you can see we catch EJBTransactionRolledbackException, why not ConstraintViolationException? Trust me it will not work because that exception is already wrapped by the container, so to be able to process it we need to unwrap it again.
Post a Comment