簡體   English   中英

使用Spring AOP時捕獲異常后不返回值

[英]Do not return value after catching exception while using Spring AOP

我使用Spring AOP進行異常處理。 我正在使用周圍的建議,因為我想記錄輸入參數。

我的方面方法是這樣的:

public Object methodName(ProceedingJointPoint pjp){
  Object returnVal = null;
   try{
  returnVal = pjp.proceed();
 } catch(Throable t) {
   // Log exception 

 }
 return returnVal ;

}

我目前面臨的問題是:當發生異常時我想記錄異常,但我不想返回null(returnval)。 可能嗎?

在沒有AOP的正常情況下:當方法中的某些行在該行之后拋出異常時,不會執行任何其他行。 我想要這樣的行為。

我們該怎么辦呢?

很好的舊的檢查異常,Java設計錯誤,只是繼續咬。

只需重新拋出throwable:

public Object methodName(ProceedingJointPoint pjp) throws Throwable {

...

 try {
   return pjp.proceed();
 } catch (Throwable t) {
   // so something with t: log, wrap, return default, ...
   log.warn("invocation of " + pjp.getSignature().toLongString() + " failed", t);
   // I hate logging and re-raising, but let's do it for the sake of this example
   throw t;
 }

見這里: https//stackoverflow.com/a/5309945/116509

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM