我有一个截取我的代码中的很多方法的方面。我想知道如果joinPoint的签名和声明类型可以为null。如果不是,那么我的try ... catch就不是必需的了。是我的try catch块吗?
换句话说,是否需要在以下代码中尝试... catch块?
private Object logPerfomanceInfo(ProceedingJoinPoint joinPoint) {
StringBuilder tag = new StringBuilder();
try {
tag.append(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
} catch (Exception e) {
// Do not do anything, let the execution continue
}
StopWatch stopWatch = new StopWatch(tag.toString());
Object result = joinPoint.proceed(); // continue on the intercepted method
stopWatch.stop();
PerformanceUtils.logInPerf4jFormat(stopWatch.getStartTime(), stopWatch.getElapsedTime(), stopWatch.getTag(), stopWatch.getMessage(), getRemoteAddress());
return result;
}
我不知道它是否可以是null,但我认为如果你关心它是否为null,而不是捕获每个Exception,你应该检查if(signature!= null) {...}。你当前的代码将吞噬任何世界末日的错误报告,假装它只是抛出了'NullPointerException'因为没有签名。 – 5gon12eder 2014-10-03 01:15:05
有些情况下,我已经看到这是必要的,但它们很少见。至少,如果你要吃掉整个异常,尤其是像Spring这样的东西,*记录警告。* – Compass 2014-10-03 03:34:00