My question is about capturing exceptions in ForAll method under Plinq
I was trying to run tasks concurently with setting max number of threads. Using enumerable .AsParallel() .WithDegreeOfParallelism(100) .ForAll(async item => await AsyncTask())
It works, but if AsyncTask throws exception the app crashes.
I have done the following test:
try
{
IEnumerable<string> enumerable = new List<string> { "st", "st" };
enumerable.AsParallel()
.ForAll(async f =>
{
try
{
throw new Exception(); // Or await AsyncTask that throws this
}
catch (Exception e)
{
e.ToString(); **// This Exception is captured**
throw e;
}
});
}
catch (Exception e) **// THIS IS NOT CAPTURED AND THE APP CRASHES**
{
e.ToString();
}
- And I would like to understand the reasons for this
- And other options to implement