views:

12

answers:

1

I'm trying to catch an Exception when an invalid value gets stored into a DataRow. I'm reading the values from a text file so anything could be stored there. I was hoping to be able to catch an InvalidCastException from the following code...

try
{
  // Store the values into the Data Row
  DataRow row = dataset.Tables["Table"].NewRow();
  for (int i = 0; i < fieldCount; i++)
    row[i] = values[i];
  dataset.Tables["Table"].Rows.Add(row);
}
catch (InvalidCastException castException)
{
  return false; // Not a serious problem...just log the issue
}
catch (Exception e)
{
  throw e; // A more serious problem occured, so re-throw the exception
}

The problem seems that storing an invalid value into the DataRow (storing "Hello" into a column defined for ints) throws a general exception (System.Exception) so doesn't get caught by my try/catch block...wasn't sure if that's in line with the MSDN documentation.

+1  A: 

OK...worked it out...

It throws an ArgumentException.

Sambo