当我插入不同类型的值时,DataRow 是否会抛出 InvalidCastException...?
当无效值存储到 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”存储到为整数定义的列中)会引发一般异常(System.Exception),因此没有被我的 try/catch 块捕获...不确定这是否符合 MSDN 文档。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的...解决了...
它抛出一个 ArgumentException 。
OK...worked it out...
It throws an ArgumentException.