I have a simple program but I am not sure why I didn't capture the AggregateException or just Exception in the caller? The following line of code: var webRequest = WebRequest.Create(url); will throw invalid url exception. But the exception is not captured in the caller code. Can anyone show me why I didn't capture the AggregateException in this case?
class Program
{
static void GetWebResponseSizeAsync(string url)
{
var webRequest = WebRequest.Create(url);
var webResponse = webRequest.GetResponse();
using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
{
string text = streamReader.ReadToEnd();
Console.WriteLine("Read {0} bytes from web site {1}", text.Length, url);
}
}
static void Main(string[] args)
{
string url = @"www.google.com";
try
{
var task = Task.Run(()=>GetWebResponseSizeAsync(url));
Console.WriteLine("....");
}
catch (AggregateException e)
{
Console.WriteLine("Catch exception: {0}", e.Message);
}
Console.WriteLine("Done");
Console.ReadLine();
}
}