我正在尝试重新创建将导致此异常的条件:
System.AggregateException: A Task's exception(s) were not observed
either by Waiting on the Task or accessing its Exception property.
As a result, the unobserved exception was rethrown by the finalizer thread.`
我写了这个程序,以为我会导致异常,但它没有:
using System;
using System.Threading.Tasks;
namespace SomeAsyncStuff
{
class Program
{
static void Main(string[] args)
{
Task.Factory.StartNew(() => { throw new NullReferenceException("ex"); });
GC.Collect();
Console.WriteLine("completed");
}
}
}
在我的实际应用程序中,我使用了 TPL,但我没有正确编写异常处理代码。结果我得到了那个例外。现在我试图在一个单独的程序中重新创建相同的条件来试验未观察到的异常。