gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:29:58 31 4
gpt4 key购买 nike

我不能使用多个 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

最佳答案

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

extends RestClientResponseException

RestClientResponseException

extends RestClientException

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

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

关于java - 错误 :(229, 12) java : exception org. springframework.web.client.HttpStatusCodeException 已被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46376581/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com