8
votes

I have a foreach loop that breaks during the loop in the condition of the foreach itself. Is there a way to try catch the item that throws the exception and then continue the loop?

This will run a few times until the exception hits and then end.

try {
  foreach(b in bees) { //exception is in this line
     string += b;
  }
} catch {
   //error
}

This will not run at all because the exception is in the condition of the foreach

foreach(b in bees) { //exception is in this line
   try {
      string += b;
   } catch {
     //error
   }
}

I know some of you are going to ask how this is happening so here is this: Exception PrincipalOperationException is being thrown because a Principal (b in my example) cannot be found in GroupPrincipal (bees).

Edit: I added the code below. I also figured out that one group member was pointing to a domain that no longer exists. I easily fixed this by deleting the member but my question still stands. How do you handle exceptions that are thrown inside the condition of a foreach?

PrincipalContext ctx = new PrincipalContext(ContextType.domain);
GroupPrincipal gp1 = GroupPrincipal.FindByIdentity(ctx, "gp1");
GroupPrincipal gp2 = GroupPrincipal.FindByIdentity(ctx, "gp2");

var principals = gp1.Members.Union(gp2.Members);

foreach(Principal principal in principals) { //error is here
   //do stuff
}
2
The problem isn't in a "condition". It's in the process of enumerating the bees object. For debugging purposes, try to "manually" enumerate bees. It's some sort of IEnumerable<T> or IEnumerable. Manually do .MoveFirst() and see if you get the exception right away. - John Saunders
Wow, I didn't realize C#'s didn't have a resume on next... I just found all these forums of guys looking for the equivalent. I suppose its just another good thing about a being a VB.net developer. My apologies mates - Ccorock
@mellamokb, Actually I don't use it too much myself. I used to work with a rather interesting C++ programmer who was forced to write in Vb.net, he would litter the damn code with On error resume next. Why would I just make something up here? - Ccorock
Can you provide your actual code, i.e. at least the actual classes being involved (is it this GroupPrincipal you mean? Because I cannot see that it is even enumerable.). Maybe we can find a more "domain specific" solution for your problem then, which could ultimately be easier than the "generic" case. - Christian.K

2 Answers

5
votes

Almost the same as the answer from @Guillaume, but "I like mine better":

public static class Extensions
{
    public static IEnumerable<T> TryForEach<T>(this IEnumerable<T> sequence, Action<Exception> handler)
    {
        if (sequence == null)
        {
            throw new ArgumentNullException("sequence");
        }

        if (handler == null)
        {
            throw new ArgumentNullException("handler");
        }

        var mover = sequence.GetEnumerator();
        bool more;
        try
        {
            more = mover.MoveNext();
        }
        catch (Exception e)
        {
            handler(e);
            yield break;
        }

        while (more)
        {
            yield return mover.Current;
            try
            {
                more = mover.MoveNext();
            }
            catch (Exception e)
            {
                handler(e);
                yield break;
            }
        }
    }
}
4
votes

Maybe you can try to create a method like that:

    public IEnumerable<T> TryForEach<T>(IEnumerable<T> list, Action executeCatch)
    {
        if (list == null) { executeCatch(); }
        IEnumerator<T> enumerator = list.GetEnumerator();
        bool success = false;

        do
        {
            try
            {
                success = enumerator.MoveNext();
            }
            catch
            {
                executeCatch();
                success = false;
            }

            if (success)
            {
                T item = enumerator.Current;
                yield return item;
            }
        } while (success);
    }

and you can use it this way:

        foreach (var bee in TryForEach(bees.GetMembers(), () => { Console.WriteLine("Error!"); }))
        {
        }