I'm trying to catch an NullReferenceException which is going to be thrown by Task.Factory.StartNew method. I think it should be caught by 'try' statement with task.Wait() method. I also referred to Why is this exception not caught?, but have no idea. Would you share your wisdom?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Csharp_study
{
class Program
{
static void Main(string[] args)
{
Task my_task = Task.Factory.StartNew(() => { throw null; });
try
{
my_task.Wait();
}
catch (AggregateException exc)
{
exc.Handle((x) =>
{
Console.WriteLine(exc.InnerException.Message);
return true;
});
}
Console.ReadLine();
}
}
}