How to cause AggregateException with TPL?

Posted by Sly on Stack Overflow See other posts from Stack Overflow or by Sly
Published on 2010-12-22T05:40:24Z Indexed on 2010/12/22 5:54 UTC
Read the original article Hit count: 350

Filed under:
|

I'm trying to recreate the conditions that will cause this exception:

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.`

I wrote this program thinking I'd cause the exception but it does not:

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");            
        }
    }
}

In my real application, I use TPL and I did not code my exception handling right. As a result I get that exception. Now I'm trying to recreate the same conditions in a separate program to experiment with unobserved exceptions.

© Stack Overflow or respective owner

Related posts about c#

Related posts about TPL