3
I have the following problem.
I need to perform a user update process and your group, and to perform the change I perform the user search, validating if it exists and soon after, I look for the group checking if it exists, in case any error happens I raise a new Exception
informing.
However, when I use the orElseThrow
group within a lambda have the return of the following error by the IDE:
Unhandled Exception: java.lang.Exception
Follows code below:
public UserDTO updateUser(UserDTO dto) throws Exception {
if (dto.getId() == null) {
throw new Exception("Usuario não informado corretamente para atualização.");
}
// buscando dados de usuario
final Optional<UserEntity> user = repository.findById(dto.getId());
user.ifPresent(u -> {
u.setEmail(dto.getEmail());
u.setStatus(dto.isStatus());
// buscando referencia de grupo
final Optional<GroupEntity> group = groupService.getGroup(dto.getGroupId());
// grupo nao encontrado
group.orElseThrow(() -> new Exception("Grupo não encontrado"));
// grupo encontrado
group.ifPresent(g -> {
u.setGroupEntity(g);
});
repository.save(u);
});
// usuario nao encontrado
user.orElseThrow(() -> new Exception("Usuario não encontrado"));
return dto;
}
You need to see the definition of
ifPresent
. I think it’s aConsumer
, that does not have in its definition the exception release– Jefferson Quesado