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?

Shail Patel
  • 1,764
  • 5
  • 30
  • 46
  • Does `forEach` throw exceptions? Maybe have a look at [How do I work with exception throwing methods in streams?](http://stackoverflow.com/questions/23548589/java-8-how-do-i-work-with-exception-throwing-methods-in-streams) – MadProgrammer Mar 01 '17 at 05:03
  • Are you sure you saved and recompiled your code? Throwing `IOException` should have made this go away. – Tim Biegeleisen Mar 01 '17 at 05:03
  • You can say your code throws this exception. But if nothing up the stack handles the exception, the program will still blow up. – D M Mar 01 '17 at 05:04
  • @MadProgrammer ah yeah that was it! i'm new to java8, thanks for your help! – Shail Patel Mar 01 '17 at 05:08
  • @ShailPatel Yea for wild guessing :P – MadProgrammer Mar 01 '17 at 05:09

1 Answers1

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?
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71