Below is the Main
method code,
static void Main(string[] args)
{
var taskList = new List<Task<bool>>();
try
{
for (var i = 0; i < 10; i++)
{
taskList.Add(Processor.Process(i));
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
And the Process
method,
public static class Processor
{
public static async Task<bool> Process(int num)
{
if (num % 2 == 0)
{
Console.Write(num);
}
else
{
throw new Exception("Hello Exception");
}
await Task.Delay(1);
return true;
}
}
The output of the program is 02468
. I have 2 questions,
Why the exception not delegating to
Main
method in case of odd number according to the code?With above code my thought that
task
is just added and it will not go for processing until I use something likevar responses = await Task.WhenAll(taskList);
, but I noticed it's processing, why is this nature, can we hold processing and instructing at once?
Thanks.