Tuesday, 10 September 2013

Exception handling in multithreaded C#

try
{
  Parallel.Invoke(
    () =>
    {
      Console.WriteLine("Starting Job 1..");
      Thread.Sleep(3*1000);
      Console.WriteLine("Starting Job 1.. SUCCESS");
    },
    () =>
    {
      Console.WriteLine("Starting Job 2..");
      throw new Exception("Job 2 Failed");
    }
  );
}
catch (AggregateException ex)
{
  Console.WriteLine(ex);
  foreach (var innerEx in ex.InnerExceptions)
  {
    Console.WriteLine(innerEx);
  }        
}

No comments:

Post a Comment