1

我很困惑为什么即使捕获到异常,此方法也总是返回......它不应该在记录错误后返回吗?

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
    }
4

2 回答 2

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
}
于 2012-08-28T18:07:49.197 回答
1

正确代码:

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

}
于 2012-08-28T18:15:32.107 回答