This is my cose:
private ResultSetType makeRequest() {
try (CloseableHttpResponse response = this.sendRequest(request)) {
String responseBody = IOUtils.toString(
response.getEntity().getContent(),
StandardCharsets.UTF_8
);
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
// do something;
return ...
} else {
throw new LoaderSystemFault(LoaderConstants.ErrorCodes.ERR_002, "Communication error with Servei Territorial (status-code: {0} / request: {1} / response: {2})", response.getStatusLine().getStatusCode(), request, responseBody);
}
} catch (IOException | JAXBException e) {
throw new LoaderSystemFault(LoaderConstants.ErrorCodes.ERR_002, "Communication error with Servei Territorial (status-code: {0} / request: {1} / response: {2})", response.getStatusLine().getStatusCode(), request, responseBody);
}
}
My goal is to close CloseableHttpResponse
.
Several questions here:
- What about
return
statement insidetry-catch-resources
? - What about
throw new
statements?
It's not clear to me if CloseableHttpResponse
will be closed regardless of whether return
or thow new
statements are reached.
How could I refactor above code?
Any ideas?