System.Data.RowNotInTableException疑惑

來源:互聯網
上載者:User
在System.Data下提供了RowNotInTableException異常類,主要用於對DataTable的DataRow操作異常。該異常類的繼承關係如下:
RowNotInTableException:DataException:SystemException:Exception
我在對一個DataTable做刪除一個不屬於它的DataRow時,按照對該異常類的理解,應該觸發行不在表中的異常,但是卻沒有觸發該異常。測試代碼如下:try
{
    DataTable dt = new DataTable() ;
    dt.Columns.Add("a");
    dt.Columns.Add("b");
    DataRow dr = dt.NewRow() ;
    dr["a"] = "a";
    dr["b"] = "b";
    dt.Rows.Add(dr);
    DataRow dr2 = dt.NewRow() ;
    dr2["a"] = "c";
    dr2["b"] = "d";
    dt.Rows.Remove(dr2) ;
}
catch(System.Data.RowNotInTableException irce)
{
    MessageBox.Show(irce.Message) ;
}
catch(System.IndexOutOfRangeException ore)
{
    MessageBox.Show(ore.Message) ;
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message); 
}

執行上面的代碼,沒有拋出RowNotInTableException,卻拋出了IndexOutOfRangeException,異常資訊為(中文):給定的資料行不在當前的DataRowCollection中。從這個異常可以看出,對DataTable進行刪除DataRow時,就是對DataTable的DataRowCollection進行刪除操作,所以拋出了IndexOutOfRangeException是正確的。只是System.Data下提供了RowNotInTableException類,為何還去拋出IndexOutOfRangeException呢?這是否說明System.Data下提供的異常類不夠完善,或者刪除DataTable的DataRow出現DataRow不在DataTable中時,拋出異常的處理有點問題呢?

哪位高手可以給以解釋一下,多謝了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.