我写这篇代码和PMD抱怨我可以实现更多优化来记录异常吗?
名称
私有方法无效xyzMethod(AnyObject anyObject)
位置
类XYZClass(xyz.package)
问题s ynopsis
正在对捕获到的异常执行实例检查。为此异常类型创建一个单独的catch子句。
try {
premium = serviceCallEjb(anfrage);
} catch (Exception e){
final int typeError;
if(e instanceof ServiceException){
typeError = CcaLogManager.ERROR;
e.printStackTrace();
} else {
typeError = CcaLogManager.WARN;
}
myAnfrage.setStatus(DcAnfrage.ProcessingError);
myAnfrage.setStatusInfo(e.getMessage());
final String message = String.format("Error in CcaMotorinsuranseCalculator, caught: %s, message: %s",e.getClass().getName(),e.getMessage());
CcaLogManager.log(typeError, message);
}
你喜欢,如果我把它写这样吗?
我可以做的任何其他优化?
try {
premium = serviceCallEjb(anfrage);
}
catch(ServiceException e){
final int typeError;
typeError = CcaLogManager.ERROR;
e.printStackTrace();
processException(e, typeError);
}
catch (Exception e){
final int typeError;
typeError = CcaLogManager.WARN;
processException(e, typeError);
}
private <T extends Exception> void processException(T e, final int typeError){
this.myObject.setStatus(/*Any number*/7);
this.myObject.setStatusInfo(e.getMessage());
final String message = String.format("Error in Some place, caught: %s, message: %s",e.getClass().getName(),e.getMessage());
Logger.log(typeError, message);
}
ServiceException是一个错误,任何其他异常是一个警告 –