December 2019
Intermediate to advanced
346 pages
9h 8m
English
Now, we'll create multiple tasks and then try to throw exceptions from them. Then, we'll learn how to list different exceptions from different tasks from the caller:
static void Main(string[] args){ Task taskA = Task.Factory.StartNew(()=> throw new DivideByZeroException()); Task taskB = Task.Factory.StartNew(()=> throw new ArithmeticException()); Task taskC = Task.Factory.StartNew(()=> throw new NullReferenceException()); try { Task.WaitAll(taskA, taskB, taskC); } catch (AggregateException ex) { foreach (Exception innerException in ex.InnerExceptions) { Console.WriteLine(innerException.Message); } } Console.ReadLine();}
Here is the output when we run the preceding code:
In the preceding code, we ...
Read now
Unlock full access