Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handled Java Exception resulting in Unhandled Exception

Tags:

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

I get an unhandled IOException exception error with lines like objectMapper.readValue(objectMapper.writeValueAsString(msgObject), MatchingTaskOperatorMetricsMsg.class));

But I am explicitly saying this method throws this exception?

like image 609
Shail Patel Avatar asked Mar 19 '26 22:03

Shail Patel


1 Answers

This warning might be showing withing the lines that's wrapped inside switch-case statements. The one that's outside it would be working fine.

The reason is because that code is inside lambda expression. Unhandled exceptions aren't handled this way in streams and lambda expressions.

You would have to wrap the code written inside lambda expression in try-catch

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: 
                    try {
                        returnMsgList.add(objectMapper.readValue(objectMapper.writeValueAsString(msgObject), MatchingTaskOperatorMetricsMsg.class));
                    }
                    catch(IOException io) {}
                case CURATE_PLACE:
                case QC_PLACE: 
                    try {
                        returnMsgList.add(objectMapper.readValue(objectMapper.writeValueAsString(msgObject), MatchingTaskOperatorMetricsMsg.class));
                    }
                    catch(IOException io) {}
            }
        });

See also:

  1. Java 8 method reference unhandled exception
  2. Java 8: How do I work with exception throwing methods in streams?
like image 95
Raman Sahasi Avatar answered Mar 21 '26 12:03

Raman Sahasi