2

I am trying to throw an exception inside lambda but it keeps giving me an error saying that Unhandled IOException.

 private  <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
        return getValue(query,
            reader -> {
                try {
                    return mapper.readValue(reader, responseClass);
                } catch (IOException e) {
                    throw new IOException("Exception while deserializing the output " + e.getMessage());
                }
        });
}

Can someone tell me what I am doing wrong ?

1
  • 2
    In any case, don't rethrow like this: provide e as a cause to the new exception (if you actually need that at all), in order to provide the stack trace of the actual exception. Commented Aug 24, 2018 at 20:03

1 Answer 1

3

The functional interface you use in getValue() doesn't specify the IOException checked exception in this signature.
So you cannot throw it as only declared checked exceptions may be thrown inside the lambda body.
Either create and use your own functional interface that declares IOException or instead throw any RuntimeException instance from the lambda, which is valid.
For example UncheckedIOException as suggested by MC Emperor.

Besides you should throw the new exception by chaining it to the cause exception to keep the information in the stracktrace:

try {
    return mapper.readValue(reader, responseClass);
} catch (IOException e) {
    throw new UncheckedIOException("Exception while deserializing the output ", e);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Or throw an UncheckedIOException.
Good answer, however it's best that junior developer learn not to default to throwing RuntimeException everywhere because it lacks clarity. You should extend RuntimeException and call it something like JsonParseException in this case. @MCEmperor also gives an elegant solution if you don't want to create your own class. However prefer domain specific classes.
@Brad Completely agree about typing. But JsonParseException exists already in Jackson. In fact the OP wants to re-throw any exception thrown by ObjectMapper.readValue() that actually throw multiple flavors of IOException : ` IOException, JsonParseException, JsonMappingException`. So the MC Emperor is probably a good hint for this usecase.
@MC Emperor very good hint.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.