no

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...

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.


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.

Related

javaee 5647978111395700514

Post a Comment Default Comments

Outsourcing

Are you looking for freelancers in the Philippines? Get in touch.

Donations

If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.

item