Continue loop iteration after exception is thrown

.net, c#, exception

Solution

Just change the scope of the `catch` to be inside the loop, not outside it:

for (int i = 0; i < 10; i++)
{
    try
    {
        if (i == 2 || i == 4)
        {
            throw new Exception("Test " + i);
        }
    }
    catch (Exception ex)
    {
        errorLog.AppendLine(ex.Message);
    }
}

Problem

Let's say I have a code like this: ``` try { for (int i = 0; i < 10; i++) { if (i == 2 || i == 4) { throw new Exception("Test " + i); } } } catch (Exception ex) { errorLog.AppendLine(ex.Message); } ``` Now, it's obvious that the execution will stop on `i==2`, but I want to make it finish the whole iteration so that in the `errorLog` has two entries (for `i==2` and `i==4`) So, is it possible to continue the iteration even the exception is thrown ?

Original source