Java 爪哇语;抛出;条款不起作用

Java 爪哇语;抛出;条款不起作用,java,exception,Java,Exception,为什么下面的代码片段 private void getEvents() throws VersionNotFoundException{ gameRepository.findAll().forEach(game->{ HttpHeaders headers = new HttpHeaders(); String appVersion = getClass().getPackage().getImplementationVersion();

为什么下面的代码片段

private void getEvents() throws VersionNotFoundException{
    gameRepository.findAll().forEach(game->{
        HttpHeaders headers = new HttpHeaders();
        String appVersion = getClass().getPackage().getImplementationVersion();
        if (appVersion==null) {
            throw new VersionNotFoundException();
        }
        headers.set("X-TBA-App-Id","4205:"+this.getClass().getPackage().getImplementationVersion());
        HttpEntity<?> requestEntity = new HttpEntity<>(headers);
        restTemplate.exchange(getEventsForYearString, HttpMethod.GET,requestEntity , Event.class, game.getYear());
    });
}

private class VersionNotFoundException extends Exception {
}
private void getEvents()引发VersionNotFoundException{
gameRepository.findAll().forEach(游戏->{
HttpHeaders=新的HttpHeaders();
字符串appVersion=getClass().getPackage().getImplementationVersion();
如果(appVersion==null){
抛出新版本NotFoundException();
}
headers.set(“X-TBA-App-Id”,“4205:”+this.getClass().getPackage().getImplementationVersion());
HttpEntity requestEntity=新的HttpEntity(标头);
restTemplate.exchange(getEventsForEarString,HttpMethod.GET,requestEntity,Event.class,game.getYear());
});
}
私有类VersionNotFoundException扩展异常{
}

在类中,导致行
抛出新版本NotFoundException()
引发编译器错误,必须捕获或声明抛出
版本NotFoundException
?它很清楚地被宣布被扔掉

传递给
gameRepository.findAll().forEach()
的lambda函数没有
抛出
。这就是错误所说的。

已检查异常不能在lambda表达式中抛出。我忘记了这是在lambda中的事实。

您要重写的lambda方法的签名中没有
VersionNotFoundException
,因此被重写的方法也不能(包括您的lambda)。由于
#forEach
接受一个不允许检查异常的消费者,因此您必须始终捕获该lambda中的异常

至于确定是否需要抛出异常,我会在
#forEach
之外完全这样做:

private void getEvents() throws VersionNotFoundException {
    String appVersion = getClass().getPackage().getImplementationVersion();
    if (appVersion == null) {
        throw new VersionNotFoundException();
    }
    gameRepository.findAll().forEach(game-> {
        HttpHeaders headers = new HttpHeaders();
        headers.set("X-TBA-App-Id", "4205:"+ appVersion);
        HttpEntity<?> requestEntity = new HttpEntity<>(headers);
        restTemplate.exchange(getEventsForYearString, HttpMethod.GET, requestEntity, Event.class, game.getYear());
    });
}
private void getEvents()引发VersionNotFoundException{
字符串appVersion=getClass().getPackage().getImplementationVersion();
如果(appVersion==null){
抛出新版本NotFoundException();
}
gameRepository.findAll().forEach(游戏->{
HttpHeaders=新的HttpHeaders();
标题集(“X-TBA-App-Id”,“4205:”+appVersion);
HttpEntity requestEntity=新的HttpEntity(标头);
restTemplate.exchange(getEventsForEarString、HttpMethod.GET、requestEntity、Event.class、game.getYear());
});
}

您应该为lambda函数定义抛出,还应该遵循Java命名约定。@LewBloch您能详细说明一下吗?我见过的大多数java命名约定都会考虑这一点。因此,它应该以小写字母开头。按照拼写,它看起来像一个类型名。@LewBloch已修复。这只是一个输入错误。他们可以,接收方法必须接受一个功能接口,允许检查exception@Rogue我找到了一个答案,我说这是不可能的。从那以后,情况发生了变化吗?您可以指定自己的功能接口,并将其用于方法。然而,这并不是完全的流兼容。我忘了我在一家foreach公司工作。在我注意到之后,我最终改变了它,或多或少是你建议的。