繁体   English   中英

当我插入其他类型的值时,DataRow是否会引发InvalidCastException?

[英]Does DataRow throw an InvalidCastException when I insert a value of a different type…?

我正在尝试将无效值存储到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文档一致

好...解决了...

它抛出一个ArgumentException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM