简体   繁体   English

在catch块中使用Continue语句

[英]Using continue statement in catch block

I have a foreach loop that is looping through an entity framework result. 我有一个foreach实体框架结果的foreach循环。 Each result is being passed through a function. 每个结果都通过一个函数传递。 So to catch any errors I have a try/catch block setup. 因此,要捕获任何错误,我需要设置try/catch块。

Something like this: 像这样:

foreach (var resetReq in query)
{
    try
    {
        Console.WriteLine("Attemtping password reset for: " + resetReq.uname);
        if (adTools.resetPassword(resetReq.uname, resetReq.agentUCID))
        {
            Console.WriteLine("Password reset for: " + resetReq.uname);
            using (var updateDB = new resetDB())
            {
                Request r = updateDB.Requests.First(x => x.id == resetReq.id);
                r.reqCompletedDate = DateTime.Now;
                r.completed = 1;

                updateDB.SaveChanges();

                Console.WriteLine("Reset record for: " + resetReq.uname + 
                                  " updated successfully to reflect completion.");
            }
        }
    }
    catch (Exception ex)
    {
        mailFunctions mailFunc = new mailFunctions();
        mailFunc.sendMail(ex);
        continue;
    }
}

My question is, will the continue statement in my catch block function properly? 我的问题是,catch块中的continue语句能否正常运行? Meaning once the exception is thrown and my mail function is fired off, will it continue to loop? 意思是一旦抛出异常并触发了我的邮件功能,它将继续循环吗?

The continue will work. continue将工作。

However, it is redundant. 但是,这是多余的。 You don't need the continue at all, since there is no code after the catch block, that needs to be skipped due to any exception being caught. 您根本不需要continue ,因为在catch块之后没有任何代码,由于catch了任何异常,因此需要跳过该代码。

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

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