错误:(229,12)java:异常org.springframework.web.client.HttpStatusCodeException已被捕获

时间:2017-09-23 06:03:19

标签: java rest spring-boot try-catch

我不能使用多个捕获?首先使用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

1 个答案:

答案 0 :(得分:1)

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

extends RestClientResponseException

RestClientResponseException

extends RestClientException

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

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