John Joe John Joe - 25 days ago 8
reST (reStructuredText) Question

Error:(229, 12) java: exception org.springframework.web.client.HttpStatusCodeException has already been caught

Can't I use multiple catch ? First catch use for

RestClientException
and second use
HttpStatusCodeException


try {
ResponseEntity<Stdo> responseEntity = restTemplate.exchange(theUrl, HttpMethod.POST, entity, Stdo.class);
}catch (RestClientException ex) {
if (ex.toString().contains("Connection timed out")) {
}
}catch(HttpStatusCodeException ex)
{
// get http status code
}
}


Error

Error:(229, 12) java: exception org.springframework.web.client.HttpStatusCodeException has already been caught

Answer Source

The hierarchy in the docs justify the error that you read. The HttpStatusCodeException

extends RestClientResponseException

and the RestClientResponseException

extends RestClientException

Hence the error. You can use those multiple catch in the reverse order.

catch(HttpStatusCodeException ex) {
    // get http status code
} catch (RestClientException ex) {
    if (ex.toString().contains("Connection timed out")) {...}
}