using System;
using System.Threading;
using System.Threading.Tasks;

namespace Ausnahmebehandlung2
{
    class Program
    {
        public static void UnreliableOperation()
        {
            Console.WriteLine("\n Aufgabe");
            Random generator = new Random();
            Thread.SpinWait(100000000);

            if (generator.Next(2) == 1)
                throw new Exception("\t Ausnahme in Aufgabe");
            else
                Console.WriteLine("\t In Aufgabe lief alles glatt");
        }

        static void Main(string[] args)
        {
            try
            {
                Task task1 = Task.Factory.StartNew(UnreliableOperation);
                Task task2 = Task.Factory.StartNew(UnreliableOperation);

                Task.WaitAll(task1, task2);
            }
            catch (AggregateException ae)
            {
                Console.WriteLine("\n Fehlerbehandlung");

                foreach (Exception ie in ae.InnerExceptions)
                {
                    Console.WriteLine(ie.Message);
                }
            }

            Console.ReadLine();
        }
    }
}
