处理您认为值得处理的所有事情,并在发生“不可能”事件时处理异常。
由于@AviewAnew说,你可能是一个有点偏执狂,但这并不意味着你错了!
我原以为缺少的列会比空值差得多,那么如何为丢失的列抛出异常呢?
try // wrap everything in a try/catch to handle things I haven't thought of
{
if (!dr.Table.Columns.Contains("column"))
{
throw new SomeSortOfException("cloumn: " + column + " is missing");
}
else // strictly don't need the else but it makes the code easier to follow
{
if (dr["column"].Equals(DBNull.Value))
{
this.value= null;
}
else
{
this.value = (type) dr["column"];
}
}
}
catch(SomeSortOfException ex)
{
throw;
}
catch(Exception ex)
{
// handle or throw impossible exceptions here
}
另一方面......如果你打算在整个代码中进行所有这些检查,维护费用将会相当可观。 ......这是另一回事。
您的电话!
这个问题确实与数据库没有多大关系。呼叫服务或DLL也可以这样说...这实际上是一个通用的异常处理问题。 – 2010-11-18 20:50:11