Hot questions for Using Enterprise JavaBeans in exception handling

Question:

I wrote a user registration service which should throw an Exception if the user already exists. Here is the code for the register method:

    public User register(User user, String role) throws UserExistsException{
    boolean userExists = existUserInDB(user);
    if(userExists) {
        logger.info("Yes, this user exists!");
        throw new UserExistsException("This user already exists in database!");
    }

    try {

        String encryptedPassword = getEncryptedPassword(user.getPassword(), getSalt(user));
        user.setPassword(encryptedPassword);

        Role userRole;

        if (role == null){
            TypedQuery<Role> query = entityManager.createQuery(
                    "SELECT r "+
                    "FROM Role r "+
                    "WHERE rolename = 'User'", Role.class);
            userRole = query.getSingleResult();
        } else {
            TypedQuery<Role> query = entityManager.createQuery(
                    "SELECT r "+
                    "FROM Role r "+
                    "WHERE rolename = '"+role+"'", Role.class);
            userRole = query.getSingleResult();
        }

        user.getRoles().add(userRole);
        userRole.getUsers().add(user);

        entityManager.persist(userRole);
        entityManager.persist(user);

    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {

        logger.warning("CryptAPI faild: " + e.getMessage());

    }

    return user;
}

The logging confirms, that the existUserInDB method returns the correct boolean value. The given user exists in the database. But the exception is never thrown. I have a lot of self implemented Exceptions in my application which all are thrown and catched correctly. All but this one. Can somebody help me with this?

And here is the code where I call the register method.

    public String register() {
    regUser.setEmail(this.emailAdd);

    try {

        userService.register(regUser, userRole);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Der Benutzer wurde erfolgreich registriert.", null));
        context.getExternalContext().getFlash().setKeepMessages(true);


    } catch (UserExistsException e) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Der Benutzer ist bereits registriert!", null));
    }
    curSession.setCurrentUser(regUser);
    init();
    return "home.xhtml?faces-redirect=true";
}

And here is the class definition of the exception:

public class UserExistsException extends Exception {

private static final long serialVersionUID = -9218609941894034576L;

public UserExistsException(){
    super();
}

public UserExistsException(String message){
    super(message);
}

}

Answer:

The problem maybe int the line below that will not do the right things which you want.

    catch (UserExistsException e) {
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Der Benutzer ist bereits registriert!", null));
    }

You can check that code executed here like this:

catch (UserExistsException e) {
     System.out.println(">>>>>>>>>>>>>>>>>>");
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Der Benutzer ist bereits registriert!", null));
        }

If the line printed, then it means the Exception has been thrown. But the rest of code did not do the things that you want from.

Question:

when trying to run weblogic.appc on my app I get the following error:

"In EJB CallerEJB, the throws clause for method hasBalance(java.lang.String) in the bean class contains exceptions that are NOT in the throws clause of the corresponding remote interface method."

This is the method in my remote interface:

public boolean hasBalance(String sin) throws RemoteException, NamingException,     CreateException, XrefAccountNumberException, AccountNotFoundException, AccountBadFormatException, ExclusiveLockException, ValidationException;

This is the method in the bean class:

public boolean hasBalance(String sin) throws RemoteException, NamingException, CreateException, XrefAccountNumberException, AccountNotFoundException, AccountBadFormatException, ExclusiveLockException, ValidationException{
...
}

I don't understand why weblogic thinks they have different exceptions.


Answer:

well I never bothered to check what I was importing when I pressed ctrl + shift + O one exception had the same name but different source