在 java 8 中使用 orElseThrow 抛出用户定义的异常时出错
Error while throwing User defined exception using orElseThrow in java 8
我正在尝试使用 orElseThrow
java 8 命令引发自定义异常。但是我收到以下编译错误:
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;
}
而我的自定义异常class,扩展RunTimeException的UserNotFoundException如下:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 9093519063576333483L;
public UserNotFoundException( String message) {
super(message);
}
}
我在 orElseThrow
语句中收到此错误:
The method orElseThrow(Supplier) in the type
Optional is not applicable for the arguments
(UserNotFoundException)
这里的问题是什么以及如何通过 orElseThrow
抛出自定义用户定义的异常?
提前致谢。
应该是供应商:
.orElseThrow(() -> new UserNotFoundException("User doesnt exist in system"));
我正在尝试使用 orElseThrow
java 8 命令引发自定义异常。但是我收到以下编译错误:
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;
}
而我的自定义异常class,扩展RunTimeException的UserNotFoundException如下:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 9093519063576333483L;
public UserNotFoundException( String message) {
super(message);
}
}
我在 orElseThrow
语句中收到此错误:
The method orElseThrow(Supplier) in the type Optional is not applicable for the arguments (UserNotFoundException)
这里的问题是什么以及如何通过 orElseThrow
抛出自定义用户定义的异常?
提前致谢。
应该是供应商:
.orElseThrow(() -> new UserNotFoundException("User doesnt exist in system"));