我有这样一段代码,虽然使用了Lombok的SneakyThrows
注解,但编译器仍然报错:Error:(65, 58) java: unreported exception java.rmi.RemoteException; must be caught or declared to be thrown:
@SneakyThrows
@Override
public Optional<Boolean> unregister() throws RemoteException {
if(registry != null) {
Arrays.asList(registry.list()).forEach(className -> {
registry.unbind(className);
});
}
return Optional.of(true);
}
调用上述方法的方法如下所示:
@SneakyThrows
public void stopDatabase() {
if(registrar == null) {
LOG.error("Database has not started");
} else {
registrar.unregister();
}
}
将代码更新为以下内容(解决问题) 但我们不想像这样改用for循环:
@SneakyThrows
@Override
public Optional<Boolean> unregister() {
if (registry != null) {
String[] classNames = registry.list();
for(int i=0;i<classNames.length;i++) {
registry.unbind(classNames[i]);
}
}
return Optional.of(true);
}
@SneakyThrows (RemoteException.class)
- undefinedunregister
方法的方法也在其上添加了SneakyThrows
注解 - undefined