2017-09-23 38 views
1

不能使用多個catch嗎?一是抓使用了RestClientException和第二次使用HttpStatusCodeException錯誤:(229,12)java:異常org.springframework.web.client.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

層次結構的文檔證明您閱讀錯誤。 的HttpStatusCodeException

extends RestClientResponseException 

RestClientResponseException

extends RestClientException 

因此,錯誤。您可以按相反的順序使用這些多個catch

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