public int deleteMultipleEntries(String[] idArray) throws Exception {
int result = dao.deleteMultipleEntries(idArray);
cache.invalidateAll(Arrays.stream(idArray).collect(Collectors.toList()));
if (result != idArray.length) {
Arrays.stream(idArray).forEach(s -> {
try {
cache.get(s);// this method throws ExecutionException if entry with id s not found
log.error("id:" + s + " was not deleted");
log.info("Deleting entry id:"+Integer.valueOf(s));
dao.deleteEntry(Integer.valueOf(s));//getting unhandled exception: java.lang.Exception error in IDE
} catch (ExecutionException e) {
log.info("id:" + s + " is deleted or it never existed");
}
});
}
return result;
}
public int deleteEntry(Integer primaryKey) {
String deleteSql = String.format(getDeleteSql(), primaryKey);
if (log.isDebugEnabled())
log.debug("Deleting Entry with Key: {}", primaryKey);
int affectedRows = getJdbcTemplate().update(deleteSql);
if (log.isDebugEnabled())
log.debug("Updated {} Rows", affectedRows);
return affectedRows;
}
在这句话时出现错误 dao.deleteEntry(Integer.valueOf(s))。
如果执行过程中出现异常 dao.deleteEntry(Integer.valueOf(s))。 捕获块不能捕获异常,因为它是专门捕获""ExecutionException "的,因此函数本身应该自动抛出异常,因为它的签名有throws语句。我写的try catch块是用来处理逻辑处理的,如果我在try catch之外写同样的语句,它不会给出任何错误。我想了解这里的行为,请帮助我。
那是因为你是在 Arrays.stream(idArray).forEach(...)把这个改成普通的foreach就可以了。
public int deleteMultipleEntries(String[] idArray) throws Exception {
int result = dao.deleteMultipleEntries(idArray);
cache.invalidateAll(Arrays.stream(idArray).collect(Collectors.toList()));
if (result != idArray.length) {
for(String s: idArray) {
try {
cache.get(s);// this method throws ExecutionException if entry with id s not found
log.error("id:" + s + " was not deleted");
log.info("Deleting entry id:"+Integer.valueOf(s));
dao.deleteEntry(Integer.valueOf(s));//getting unhandled exception: java.lang.Exception error in IDE
} catch (ExecutionException e) {
log.info("id:" + s + " is deleted or it never existed");
}
}
}
return result;
}