I have a parallel.Foreach loop in my code and I am wondering how to handle exceptions. Should I catch and handle(e.g write to log) exceptions inside the loop or should I catch aggregate exception outside - encapuslate the loop in try/catch?
Best regards
Should I catch and handle exceptions inside the loop or should I catch aggregate exception outside
Those two are not functionally equivalent. Both can be done, and in different ways.
But the more fundamental question is: when one or more iterations suffer an exception, do you want the remaining items to be processed or not?
If yes, then handle them inside the loop, possibly storing them like in the MSDN example.
If not, just put a try/catch around the Parallel loop itself.