我有一個截取我的代碼中的很多方法的方面。我想知道如果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