Can somebody explain the behaviour of this code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world");
try
{
Parallel.ForEach(new[] { 1, 2, 3 }, async i =>
{
await Task.Delay(TimeSpan.FromSeconds(3));
throw new TaskCanceledException();
});
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Goodbye cruel world");
Console.ReadLine();
}
}
How can it be possible, the exception pops up in the main thread out of "try" block and application fall. I knew the best way for parallel async is "Task.WhenAll". The goal of the question is to understand the behaviour.