Why does this not every complete? Instead it throws an exception, like the exception wasn't caught. Moreover, the exceptions "Index out of bounds of array" doesn't make sense to me.
int n = 3;
string[] names = new string[] { "sally", "fred", "gina" };
Task[] myTasks = new Task[n];
for (int y = 0; y < n; y++)
{
myTasks[y] = Task.Factory.StartNew(() =>
{
Action<string> action = (name) =>
{
try
{
throw new Exception(name + ", I bet you can't catch me.");
}
catch (Exception e)
{
//I caught you... didn't I?
}
};
action(names[y]);
});
}
Task.WaitAll(myTasks);
Console.WriteLine("All tasks complete.");//This line is never reached;