当无效值存储到DataRow中时,我正在尝试捕获异常。我正在从文本文件中读取值,因此可以存储任何内容。我希望能够从以下代码中捕获InvalidCastException ...
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
}
问题似乎是将无效值存储到DataRow中(将“Hello”存储到为int定义的列中)会引发一般异常(System.Exception),因此不会被我的try / catch块捕获...不确定这是否与MSDN documentation一致。