简体   繁体   English

从捕获块常规返回

[英]Returning from a catch block groovy

I'm confused as to why this method always returns even when an exception is caught... shouldn't it return after logging the error? 我对为什么即使捕获到异常也总是返回此方法感到困惑...记录错误后它不应该返回吗?

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
    }

Yes, it should return after logging the error, this is probably the error in your logger configuration if indeed the exception is thrown. 是的,它应该在记录错误后返回,如果确实引发了异常,则这可能是记录器配置中的错误。

If you want execution to stop in the catch block then return from there 如果要在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
}

Correct code: 正确的代码:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM