Invoke the given action for each element in IEnumerable values. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Invoke the given action for each element in IEnumerable values.

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*from w  w w. ja  v a 2 s .c  om*/

public class Main{
        /// <summary>
        /// Invoke the given action for each element in values.
        /// </summary>
        public static void ForEachWithExceptionMessage<T>(this IEnumerable<T> values, Action<T> action)
        {
            foreach (var v in values)
            {
                try
                {
                    action(v);
                }
                catch (AggregateException ex)
                {
                    throw new Exception("Error while handling " + v + ": " + ex.Flatten().InnerException.Message, ex);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error while handling " + v + ": " + ex.Message, ex);
                }
            }
        }
}

Related Tutorials