Gnanavel Sekar

Gnanavel Sekar

  • NA
  • 8.4k
  • 7.3m

Want to handle child task Exception in Parent Task

Jul 17 2017 7:03 AM
Hi Friends,
 
In below example i have created few child task under  Task1Async method, and assume am getting error (divide by zero exception) in Task3Async, if once i got the exception anywhere in child task means i need to stop on Task1Async();(Main Method itself) on but it continuously running to next line as well as i can't catch the child task exceptions here... 
 
 
class Program
{
public static void Main(string[] args)
{
try
{
var task1 = Task.Run(() =>
{
Task1Async();
});
Console.WriteLine("All tasks are finished");
}
catch (AggregateException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex1)
{
Console.WriteLine(ex1.Message);
}
}
static async Task Task1Async()
{
Console.WriteLine("Am Task1");
await Task2Async();
}
static async Task Task2Async()
{
Console.WriteLine("Am Task2");
await Task3Async();
}
static async Task Task3Async()
{
Console.WriteLine("Am Task3");
int i = 0;
i = 10 / i;
await Task4Async();
}
static async Task Task4Async()
{
Console.WriteLine("Am Task4");
await Task5Async();
}
static async Task Task5Async()
{
Console.WriteLine("Am Task5");
}
}
 
 
Thanks in Advance. 
 

Attachment: ChildTask_Check.zip

Answers (1)