C# .NET - Exception and complete looping - Asked By mostafa hamdy on 24-Mar-11 05:21 AM

Hello all
I have some applicataion have some method , which loop through list of object using foreach , and inside the loop I add every object to the database , but in some cases some exception happend with some object in the list and the method catch the exception and doesnt   complete the rest of the list of object, what I want is if some exception happend the with some object the looping complete with the other remaining objects my code is some thing like the following:
foreach(Product obj in lstProduct)
{
try
{
obj.Add();
}
catch(Exceprion ex)
{
string Err = ex.Message;
}
}

,please if any body get what I want and can help me please send me or tell me about some URL may help me in doing that
Mash B replied to mostafa hamdy on 24-Mar-11 05:29 AM
Try below

foreach(Product obj in lstProduct)
{
try
{
obj.Add();
}
catch(Exceprion ex)
{
string Err = ex.Message;
continue;
}
}
Kirtan Patel replied to mostafa hamdy on 24-Mar-11 05:35 AM
foreach(Product obj in lstProduct)
{
  try
  {
  obj.Add();
  }
  catch
  {
   
  }
}

instead of capturing error inherited from Exception class write catch{} this will be better :)