当前位置: 首页 > 知识库问答 >
问题:

为什么我会收到下面的“未处理异常”?[重复]

谭昱
2023-03-14
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;
        }

获取此语句时出错。deleteEntry(Integer.valueOf);

如果在执行dao.delete项(Integer.valueOf(s))时发生异常;捕捉块不能捕捉异常,因为它捕捉""执行异常"具体而言,因此函数本身应该自动抛出异常,因为它的签名已抛出语句我写的捕捉块是用于处理逻辑处理的,如果我在try捕捉之外写同样的语句,它不会给出任何错误。我想了解这里的行为。请好心帮忙

共有1个答案

端木宏盛
2023-03-14

那是因为你在rrays.stream(idArray)。将此更改为正常的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;
} 
 类似资料:
  • 问题内容: 请问为什么第13行中的错误是未报告的异常,必须在声明声明为pr的情况下将其捕获 问题答案: 您需要向引发异常的方法中添加一个,如上所述,以及调用该方法的所有方法

  • 请问为什么第13行的错误是未报告的异常,必须捕获pr声明要抛出

  • 到目前为止,我有这个: 和这个: 当我测试这个时,它不能采取双倍数字,我收到这个消息: 我该如何解决这个问题?

  • 问题内容: 要编译此代码,我可以: 将我的通话置于try / catch块中,或 已经声明它可以抛出一个。 为什么我必须这样做? (示例代码来自Kathy Sierra的SCJP书 。) 我知道引发的异常是已检查的异常,因此我必须处理它,但是在什么情况下需要引发此异常? 问题答案: 如果以一种可以引发检查异常的方式声明方法(不是的子类),则调用该方法的代码必须在一个块中调用它,否则调用者方法必须声

  • 问题内容: 我已经找到了这段代码,无法找出解决方法。在运行代码时,为什么不提示用户输入而不是Java确定没有输入?错误跟踪如下。 这是错误- 问题答案: 简单的答案是当您关闭扫描仪时- 基础输入流也会关闭:http : //docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close() 要修复此问题,请在主菜单中创建一次:

  • 我不明白为什么我收到以下代码的警告: 未选中对<code>isAssignableFrom(类)的调用 当我使用< code>isInstance方法时(根据我的理解,它提供相同的结果),我没有得到警告: