-2
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;
        }

Getting error at this statement dao.deleteEntry(Integer.valueOf(s));

if an exception occurs while executing dao.deleteEntry(Integer.valueOf(s)); the catch block cannot catch the exception since it catches ""ExecutionException" specifically, Hence the function itself should throw exception automatically since its signature has throws statement. the try catch block i have written is for handling logic handling, if i write the same statement outside the try catch, it doesn't give any error. I want to understand the behavior here. please kindly help

nikhil kekan
  • 532
  • 3
  • 16

1 Answers1

0

Thats because you are in Arrays.stream(idArray).forEach(...) Change this to normal foreach and it would work.

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;
} 
A.K.G
  • 124
  • 6