處理您認爲值得處理的所有事情,並在發生「不可能」事件時處理異常。
由於@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