0

I want to report about Exceptions that are thrown during execution of some tasks:

//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);
    }
}

However, I cannot use foreach loop in that case.

Is there any workaround that issue?

gene
  • 2,098
  • 7
  • 40
  • 98

1 Answers1

1

InnerException isn't a collection or Exceptions so you can't enumerate over it, you should use InnerExceptions. This article shows how to correctly handle the exceptions: https://msdn.microsoft.com/en-us/library/dd537614(v=vs.110).aspx

DoctorMick
  • 6,703
  • 28
  • 26
  • But in that case, I still do not know which exception can be thrown. That example uses `if`statement to kind of guess which exception is thrown. What if I have 10 exception? Should I use `if` 10 times? That's why I though I could use `foreach` – gene Oct 13 '16 at 16:12
  • Ok, got it. Thx – gene Oct 13 '16 at 16:30