A day with .Net

My day to day experince in .net

Exception Handling with the Task Parallel Library

Posted by vivekcek on August 19, 2012

We use the AggregateException class to handle exception in task’s

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExceptionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Task<int> t1 = new Task<int>(() => Add());
            try
            {
                t1.Start();
                t1.Wait();
            }
            catch (AggregateException ag)
            {
                Console.WriteLine(ag.InnerExceptions[0].Message);
            }
            Console.ReadLine();
        }
        static int Add()
        {
            
                
            throw new ArgumentException("Exception");
            
           
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s