3
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?

5
  • Does forEach throw exceptions? Maybe have a look at How do I work with exception throwing methods in streams? Commented Mar 1, 2017 at 5:03
  • Are you sure you saved and recompiled your code? Throwing IOException should have made this go away. Commented Mar 1, 2017 at 5:03
  • You can say your code throws this exception. But if nothing up the stack handles the exception, the program will still blow up. Commented Mar 1, 2017 at 5:04
  • @MadProgrammer ah yeah that was it! i'm new to java8, thanks for your help! Commented Mar 1, 2017 at 5:08
  • @ShailPatel Yea for wild guessing :P Commented Mar 1, 2017 at 5:09

1 Answer 1

2

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?
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.