从一个陷阱groovy返回

时间:2012-08-28 18:01:37

标签: 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
    }

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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

}