2012-08-28 32 views
0

我很困惑,爲什麼這個方法總是返回,即使發生了一個異常...不應該記錄錯誤後返回?從一個catch塊groovy返回

private def sendConfCode(sendTo) 
    { 

     def confCode = genConfCode() 
     try 
     { 
      mail.sendMessage(sendTo.toString(), "Your confirmation code is", confCode) 
      //insert confCode into temporary banner table? 
     } 
     catch(Exception e) 
     { 
      logIt.writeLog(e.message, Priority.ERR) 
     } 
     return confCode + " - " + sendTo 
    } 
+0

你怎麼知道會拋出異常?應用系統並查看異常是否被捕獲,如果是,那麼它是您的記錄器的錯誤 – Ankur

+0

爲什麼*在異常被捕獲後不會返回?或者你的意思是它是特別*不*寫信息(並沖洗它等) –

+0

我知道我得到一個例外。我想要執行停止在catch塊中。我覺得自己很愚蠢......重新調整了方法,並讓它做我想做的事情。 – innov83r

回答

0

正確的代碼:

private def sendConfCode(sendTo) 
    { 

     def confCode = genConfCode() 
     def sent=false 
     try 
     { 

      mail.sendMessage(sendTo.toString(), "Your confirmation code is", confCode) 
      //insert confCode into temporary banner table? 
      sent = true 
     } 
    catch(Exception e) 
    { 
     logIt.writeLog(e.message, Priority.ERR) 
    } 
    return sent 

} 
1

是的,它應該記錄錯誤後返回,這可能是在你的記錄器配置中的錯誤,如果確實拋出異常。

如果你想停止執行在catch塊然後從那裏返回

private def sendConfCode(sendTo) 
    { 

     def confCode = genConfCode() 
     try 
     { 
      mail.sendMessage(sendTo.toString(), "Your confirmation code is", confCode) 
      //insert confCode into temporary banner table? 
     } 
     catch(Exception e) 
     { 
      logIt.writeLog(e.message, Priority.ERR) 
      return 
     } 
     return confCode + " - " + sendTo 
}