Jon Skeet says
The output shows Fetched the task before it fails. The exception has been thrown synchronously before that output is written, because there are no await expressions before the validation
Is there any guarantee about the fact the exception has been thrown synchronously before that output is written ?
Why cannot be the output text(Fetched the task) written out at first?
async Task Main()
{
Task<int> task = ComputeLengthAsync(null);
Console.WriteLine("Fetched the task");
int length = await task;
Console.WriteLine("Length: {0}", length);
}
static async Task<int> ComputeLengthAsync(string text)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
await Task.Delay(5000);
return text.Length;
}