0

I have a foreach loop that is looping through an entity framework result. Each result is being passed through a function. So to catch any errors I have a try/catch block setup.

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? Meaning once the exception is thrown and my mail function is fired off, will it continue to loop?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
ITninja04
  • 53
  • 2
  • 5

1 Answers1

4

The continue will work.

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.

bit
  • 4,407
  • 1
  • 28
  • 50