Currently my signup looks like this:
public void signup(User newUser) throws Exception {
log.info("Sign up: " + newUser.getEmail());
if (restService.emailAlreadyExists(newUser.getEmail())) {
throw new Exception("Email already in use.");
}
List<Role> roles = new ArrayList<Role>();
roles = roleRepository.findAllOrderedByName();
roles.add(roleRepository.findByName("user"));
newUser.setRoles(roles);
newUser.setPassword(restService.getHashedValue(newUser.getPassword()));
try {
em.persist(newUser);
} catch (Exception e) {
throw new Exception("Just noobs use apps with less bugs. Try again. Now!");
}
log.info(newUser.toString());
userEvent.fire(newUser);
}
In first order I'm just interested in two messages (will become FacesMessage
) for the user. To prevent other cryptic messages for the user, I even would need to extend the try
-block up to roles.
Well, that would be bad practice, I guess. Also using a generic Exception
smells, they say. But: I detect following documented Exception
s in this small piece of code:
IllegalStateException
IllegalArgumentException
EntityExistsException
TransactionRequiredException
ObserverException
Even not speaking about the eight(!) Exception
s of the method getSingleResult()
of javax.persistence.TypedQuery
.
Should I really handle all Exception
s in this example, or is it ok to skip a few (and/or maybe even use a generic Exception
like above).