This question already has an answer here:
- Task.WhenAny and Unobserved Exceptions 2 answers
In the following code "Inside catch block" is never printed. "Final line" is printed though. Why so? Please help.
Task task1 = Task.Factory.StartNew(() =>
{
throw new Exception("some exception");
});
try
{
Task.WaitAny(new Task[] { task1 });
}
catch(Exception e)
{
Console.WriteLine("Inside catch block.");
}
Console.WriteLine("Final line.");
This is because your code executing in a different thread,but you can catch the exception occurred in a different thread using the following code