If I have the following code:
List<MyClass> list = GetList();
list.ForEach(i => i.SomeMethod());
and let’s say SomeMethod() throws an exception. Does ForEach continue iterating, or does it just stop right there?
If it does terminate, is there any way to get the rest of the items in the collection to run their methods?
Yes, if an exception is thrown, the loop exits. If you don’t want that behaviour, you should put exception handling into your delegate. You could easily create a wrapper method for this:
To be honest, I would try to avoid this if possible. It’s unpleasant to catch all exceptions like that. It also doesn’t record the items that failed, or the exceptions etc. You really need to think about your requirements in more detail:
It would almost certainly be cleaner to create a separate method which used the normal
foreachloop instead, handling errors and collecting errors as it went. Personally I generally prefer usingforeachoverForEach– you may wish to read Eric Lippert’s thoughts on this too.