无法使用 "foreach" 处理异常,因为异常不包含 'GetEnumerator'

Cannot use "foreach" to go through exceptions because Exception does not contain 'GetEnumerator'

我想报告 Exceptions 在执行某些任务期间抛出的问题:

//waiting for all the tasks to complete
try
{
    Task.WaitAll(task1, task2, task3);
}
catch (AggregateException ex)
{
    //enumerate the exceptions
    foreach (Exception inner in ex.InnerException)
    {
        Console.WriteLine("Exception type {0} from {1}", inner.GetType(), inner.Source);
    }
}

但是,在那种情况下我不能使用 foreach 循环。

这个问题有什么解决方法吗?

InnerException 不是集合或异常,因此您不能枚举它,您应该使用 InnerExceptions。本文介绍如何正确处理异常:https://msdn.microsoft.com/en-us/library/dd537614(v=vs.110).aspx