You do have a Task
with an exception (the one returned by GetStringAsync
). However, the await
is observing the Task
exception, which then propagates out of the DownloadAsync
method (which is async void
).
Exceptions propagating out of async void
methods behave differently; they are raised on the SynchronizationContext
that was active when the async void
method started (in this case, a thread pool SynchronizationContext
). This is not considered an unobserved exception.
If you change DownloadAsync
to return Task
, then you will have an actual unobserved Task
exception, which will be ignored (correctly):
static void Main(string[] args)
{
DownloadAsync("http://an.invalid.url.com");
Console.ReadKey();
}
async static Task DownloadAsync(string url)
{
using (var client = new System.Net.Http.HttpClient())
{
string text = await client.GetStringAsync(url);
Console.WriteLine("Downloaded {0} chars", text.Length);
}
}