I am trying to throw an custom excpetion using orElseThrow java 8 command. However I am getting the following compile errors:
public class UserInvitationServiceImpl implements UserInvitationService{
@Autowired
private UserRepository userRepository;
@Override
public void inviteUser(String email) throws UserNotFoundException {
// TODO Auto-generated method stub
if(email!= null && !email.isEmpty()) {
Optional<User> optionalUser = userRepository.findByEmail(email);
optionalUser.orElseThrow(new UserNotFoundException("User doesnt exist in system"));
}
}
}
public interface UserInvitationService {
void inviteUser(String email) throws UserNotFoundException;
}
And my custom exception class, UserNotFoundException which extends RunTimeException is as follows:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 9093519063576333483L;
public UserNotFoundException( String message) {
super(message);
}
}
I am getting this error at orElseThrow statement:
The method orElseThrow(Supplier) in the type Optional is not applicable for the arguments (UserNotFoundException)
What is the issue here and how to throw custom user defined exception via orElseThrow?
Thanks in advance.