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.