System. Data. RowNotInTableException

Source: Internet
Author: User
The RowNotInTableException class is provided under System. Data, which is mainly used for DataRow operation exceptions of DataTable. The inheritance relationship of the exception class is as follows:
RowNotInTableException: DataException: SystemException: Exception
When I delete a DataRow that does not belong to a DataTable, according to my understanding of this exception class, I should trigger an exception that is not in the table but did not trigger this exception. The test code is as follows: try
{
DataTable dt = new DataTable ();
Dt. Columns. Add ("");
Dt. Columns. Add ("B ");
DataRow dr = dt. NewRow ();
Dr ["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 );
}

When the above code is executed, RowNotInTableException is not thrown, but IndexOutOfRangeException is thrown. The exception message is (Chinese): the given data row is not in the current DataRowCollection. From this exception, we can see that when DataRow is deleted from the DataRowCollection of the DataTable, IndexOutOfRangeException is thrown correctly. Only the RowNotInTableException class is provided under System. Data. Why should I throw IndexOutOfRangeException? Does this indicate that the exception classes provided in System. Data are not perfect, or that the DataRow of the DataTable cannot be deleted when DataRow is not in the DataTable, which may cause some problems in exception handling?

Which of the following experts can explain this question? Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.