【发布时间】:2021-09-23 07:16:17
【问题描述】:
如何在我使用 java11 的 catch 块下重构 代码
public String methodName(ClassRequest request, Destination ABC) {
try {
<Some Code Here>
} catch (Exception e) {
log.error("error", ABC, e);
if(e instanceof ABCRestException ||
(ABC == PREM && (e instanceof HttpServerErrorException || e instanceof HttpClientErrorException))) {
throw e;
} else if(e instanceof HttpServerErrorException) {
throw new ABCRestException(request.getAId(), "unexpected_error", "Some Message", e, INTERNAL_SERVER_ERROR);
} else if(e instanceof HttpClientErrorException) {
throw new ABCRestException(request.getAId(), "missing_field", "Some Message", e, BAD_REQUEST);
} else {
throw new ABCRestException(request.getAId(), "unexpected_error", "Some Massage", e, INTERNAL_SERVER_ERROR);
}
}
}
我如何重构这段代码意味着 catch 块
【问题讨论】:
-
添加单独的 catch 块
-
我可以定义一个catch块吗?并在 catch 块中声明所有异常
-
@PallaviSingh 这不是正确的方法,catch 是专门针对异常的,所以你不需要使用 inistance of
-
@PallaviSingh 你可以做的是调用 catch 块内的方法传递异常并在那里运行一些通用逻辑
-
@PallaviSingh 是的,这就是它抱怨的原因