why the exception is not caught?
Posted
by
Álvaro García
on Stack Overflow
See other posts from Stack Overflow
or by Álvaro García
Published on 2013-10-18T15:31:25Z
Indexed on
2013/10/18
15:54 UTC
Read the original article
Hit count: 151
I have the following code:
List<MyEntity> lstAllMyRecords = miDbContext.MyEntity.ToList<MyEntity>();
foreach MyEntity iterator in lstMainRecord)
{
tasks.Add(
TaskEx.Run(() =>
{
try
{
checkData(lstAllMyRecords.Where(n => n.IDReference == iterator.IDReference).ToList<MyEntity>());
}
catch CustomRepository ex)
{
//handle my custom repository
}
catch (Exception)
{
throw;
}
})
);
}//foreach
Task.WaitAll(tasks.ToArray());
I get all the records from my data base and in the foreach loop, I group all the records that have the same IDReference. Thenk I check if the data is correct with the method chekData.
The checkData method throw a custom exception if something is wrong. I would like to catch this exception to handle it.
But the problem is that with this code the exceptions are not caught and all seem to work without errors, but I know that this is not true.
I try to check only one group of records that I know that has problems. If I check only one group of registrers, the loop is execute once and then only task is created. In this case the exception is caught, but if I have many groups, then any exception s thrwon.
Why when I only have one task the exception is caught and with many groups are not?
Thanks.
© Stack Overflow or respective owner