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 Answer 1
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);
}
4 Comments
UncheckedIOException.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.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.Explore related questions
See similar questions with these tags.
eas a cause to the new exception (if you actually need that at all), in order to provide the stack trace of the actual exception.