1

I got Unhandled exception works differently on two different methods on the 2nd method getByIds Which is making no sense. I call the first method in the second method and put the try catch already.

Any idea for this exception? Thanks

@Override
public PostPayload getById(@NotNull UUID issueId) throws APIException {
    try (...) {
        return test.apply(responseIssue, issueAuxiliaryData);
    } catch (IOException e) {
        logger.error("Event='Unable to retrieve XXX ', issueId={}", issueId, e);
        throw new APIException("Unable to retrieve XXX  for issueId=" + issueId, e);
    }
}

@Override
public List<PostPayload> getByIds(@NotNull Set<UUID> issueIds) throws APIException {
    return issueIds.parallelStream()
            .map(issueId ->  {
                try {
                    return this.getById(issueId, channelId, false);
                } catch (IOException | APIException e) {
                    logger.error("Event='Unable to retrieve XXX ', issueId={}", issueId, e);
                    throw new APIException("Unable to retrieve XXX  for issueId=" + issueId, e);

                }
            })
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
}
Progman
  • 16,827
  • 6
  • 33
  • 48
newBike
  • 14,385
  • 29
  • 109
  • 192
  • 1
    Please edit your question to include the full exception message. – Progman Jun 09 '19 at 19:27
  • you cannot throw a checked exception from the lambda in your second example – Svetlin Zarev Jun 09 '19 at 19:54
  • Possible duplicate of [How can I throw CHECKED exceptions from inside Java 8 streams?](https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams) – Hadi J Jun 09 '19 at 20:21

1 Answers1

3

You can do two things with an exception:

  1. Handle it somehow
  2. Re-throw it

Your first method has a throws APIException in its signature, so throwing APIException is a valid thing to do.

But how is this different from your other method ? There you are trying to throw the exception from within a lambda passed to a stream().map() method. From the documentations we can find out the functional interface corresponding to this lambda:

public interface Function<T, R> {
    R apply(T t);
}

From the signature we can see that it does not throw any checked exceptions, so it's a compilation error to throw APIException from within the lambda (assuming APIException is a checked exception)

A possible workaround is to define another version of your exception, that derives from RuntimeException, for instance UncheckedApiException. Then you can wrap the whole stream operation in one big try-catch block adn in the catch block you can throw the checked version:

@Override
    public List<PostPayload> getByIds(@NotNull Set<UUID> issueIds) throws APIException {
        try {
            return issueIds.parallelStream()
                    .map(issueId -> {
                        try {
                            return this.getById(issueId, channelId, false);
                        } catch (IOException | APIException e) {
                            logger.error("Event='Unable to retrieve XXX ', issueId={}", issueId, e);
                            throw new UncheckedApiException("Unable to retrieve XXX  for issueId=" + issueId, e);

                        }
                    })
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
        } catch (UncheckedApiException e) {
            throw new APIException(e);
        }
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82