【发布时间】:2020-02-06 10:50:47
【问题描述】:
我正在使用弹簧。
我有一个包含多种方法的类。在每种方法中我都必须写:
public void method1(){
try{
//anything
}
catch(Exception e){
Log.error(e);
throw e;
}
}
public void method2(){
try{
//anything
}
catch(Exception e){
Log.error(e);
throw e;
}
}
public void method3(){
try{
//anything
}
catch(Exception e){
Log.error(e);
throw e;
}
}
public void method4(){
try{
//anything
}
catch(Exception e){
Log.error(e);
throw e;
}
}
我可以写一些不必在每个方法中都写的东西吗?也许是注释?
【问题讨论】:
-
在 spring 中检查
@ControllerAdvice注释。链接:spring.io/blog/2013/11/01/exception-handling-in-spring-mvc -
您预计会出现什么样的异常?如果我发现有人在代码审查期间发现了普通的异常,我通常会拒绝它。如果你不处理,你为什么要抓住它们?
-
我必须在此处记录每个异常。如果我们对我为什么在这里这样做进行抽象,是否有一种解决方案可以不在所有方法中重复这种尝试和捕获?
-
那么为什么不简单地在调用方法上捕获它们呢?