我不能使用多个捕获?首先使用RestClientException
和第二次使用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:(229, 12) java: exception org.springframework.web.client.HttpStatusCodeException has already been caught
答案 0 :(得分:1)
文档中的层次结构证明了您阅读的错误。
HttpStatusCodeException
extends RestClientResponseException
extends RestClientException
因此错误。您可以按相反的顺序使用多个catch
。
catch(HttpStatusCodeException ex) {
// get http status code
} catch (RestClientException ex) {
if (ex.toString().contains("Connection timed out")) {...}
}