Customize error message

Asked

Viewed 677 times

3

I forced an error on my system that contains the following message:

javax.persistence.Persistenceexception: org.hibernate.Exception.Constraintviolationexception: could not execute statement

It is a violation of Constraint. How can I customize to display an example in-depth message: Warning: This name already exists in the database.

I even created a class for Messages and has this method:

public static void erro(String mensagem) {

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, mensagem, "");
        FacesContext.getCurrentInstance().addMessage("messagePanel", msg);


    }

Thanks in advance.

2 answers

5

You can identify the Exception ConstraintViolationException in a catch and customize the return of a message to that error.

    } catch (ConstraintViolationException e) {
        e.printStackTrace();
    }
  • And how I put the message I want?

3


You have to treat this exception.

try {
    cadastrar();
} catch (ConstraintViolationException e) {
    erro("Este nome já existe no banco de dados");
    // e.printStackTrace();
}

Give a read on Exceptions, because that’s what you need. This link can help.

  • Can I put more than one treatment in the same catch? Since several types of error may occur.

  • @Douglas You can put other types of Exception in the catch, you can treat each type of Exception in one way, or the commonly used Exception.

  • @Douglas As Matheus said, yes, you can. As many and any you want, I believe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.