我試圖捕捉將由Task.Factory.StartNew方法引發的NullReferenceException。我認爲它應該通過task.Wait()方法的'try'語句來捕獲。我也提到Why is this exception not caught?,但不知道。你會分享你的智慧嗎?AggregateException未捕獲Task.Wait()
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();
}
}
}
謝謝。事實上,我在3個月前自己找到答案,因爲我在下面的一個發表了評論。但我找不到解決問題的方法。所以我把你的標記爲一個。祝你今天愉快! –