<分区>
我正在使用 java,我代码中的所有异常都派生自 java.lang.exception。将所有内容包含在一个 try-catch 中是个好主意吗?为每个困难的功能添加 try-catch 是个好主意吗?
因为我想让我的代码以惰性方式运行得更好,这意味着没有异常会导致我的程序崩溃,例如,捕获 IndexOutOfBoundsException、ArrayIndexOutOfBoundsException、SQLGrammarException ......
以下是我的代码:
public MyResult function1(){
MyResult myResult = new MyResult();
try{
//all codes here
}catch(Exception e){
LOGGER.error(e);
myResult.setException(e);
}finally{
return myResult;
}
}
//more same functions using same try-catch here
public MyResult functionN(){
MyResult myResult = new MyResult();
try{
//all codes here
}catch(Exception e){
LOGGER.error(e);
myResult.setException(e);
}finally{
return myResult;
}
}