HttpStatusCodeException

HttpStatusCodeException

我不能使用多重捕获吗?第一次使用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

最佳答案

文档中的层次结构证明了您所阅读的错误。
 HttpStatusCodeException

extends RestClientResponseException


RestClientResponseException

extends RestClientException


因此,错误。您可以相反的顺序使用多个catch

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

07-28 00:59