处理Java异常导致未处理的异常

时间:2017-03-01 05:01:54

标签: java

public List<OpBase> getHistory(ESRequest historyRequest)
        throws IOException, ResourceNotFoundException, URISyntaxException {
    String url = baseUrl + opIndex + "/_search";
    String resp = makeESQuery(historyRequest, url);
    HistoryResponse historyResponse = objectMapper.readValue(resp, HistoryResponse.class);
    List<OpBase> returnMsgList = new ArrayList<>();
    historyResponse.tasks.tasks.forEach(editTaskMsgResponse -> {
            Map<String, Object> msgObject = editTaskMsgResponse.source.msg;
            String taskType = (String) msgObject.get("task_type");
            EditTaskType editTaskType = EditTaskType.valueOf(taskType);
            switch(editTaskType) {
                case MIGRATE_PLACE: returnMsgList.add(objectMapper.readValue(
                        objectMapper.writeValueAsString(msgObject), MatchingTaskOperatorMetricsMsg.class));
                case CURATE_PLACE:
                case QC_PLACE: returnMsgList.add(objectMapper.readValue(objectMapper.writeValueAsString(msgObject),
                        MatchingTaskOperatorMetricsMsg.class));
            }
        });
    return returnMsgList;
}

我使用objectMapper.readValue(objectMapper.writeValueAsString(msgObject), MatchingTaskOperatorMetricsMsg.class));

这样的行获得了未处理的IOException异常错误

但是我明确地说这个方法会抛出这个异常吗?

1 个答案:

答案 0 :(得分:2)

此警告可能会显示包含在 private void Grid_Loaded_1(object sender, RoutedEventArgs e) { Grid grd = sender as Grid; DependencyObject parent = grd; while (!((parent = VisualTreeHelper.GetParent(parent)) is Window)) ; if (parent == null) return; grd.Width = (parent as Window).ActualWidth / 2; } 语句中的行。在它之外的那个工作正常。

原因是因为该代码位于switch-case表达式中。未处理的异常不会以这种方式在stream和lambda表达式中处理。

您必须将lambda表达式中编写的代码包装在lambda

try-catch

另见:

  1. Java 8 method reference unhandled exception
  2. Java 8: How do I work with exception throwing methods in streams?