datareader失败,但不会引发异常c#
本文关键字:异常 失败 datareader | 更新日期: 2023-09-27 18:28:21
我是C#的新手,我发现一些异常处理或缺乏异常处理令人困惑。我试图抓住这些例外,但没有抓住,我不明白为什么。代码将在异常后结块,异常后的所有内容都将被忽略,执行的下一行将是表单的绘制事件。
显然,我可以很容易地修复这个例子,但我想知道为什么没有引发异常。我经常看到这种情况。
这里有一个例子:
public DataTable GetTaskDescription( string EstmateNameGuid)
{
string strJobNumName = null;
MySqlCommand cmd = null;
MySqlDataReader dr = null;
MySqlConnection myconnection = new MySqlConnection(clsGlobeVar.localDB);
myconnection.Open();
DataTable table1 = new DataTable("EstGuids");
string select = GetTaskDescription_SQLCreate();
try
{
cmd = new MySqlCommand(select, myconnection);
GetTaskDescription_ParameterFill(ref cmd, clsGlobeVar.ClientGuid, EstmateNameGuid);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
table1.Columns.Add("Estimate_Guid");
table1.Columns.Add("Estimate_Description");
table1.Columns.Add("Estimate_Date");
table1.Columns.Add("Active");
string [] field = new string[5] ;
while (dr.Read())
{
// going to read field in the row.
field[0] = dr.SafeGetString(0);
field[1] = dr.SafeGetString(1);
field[2] = dr.SafeGetString(2);
field[3] = dr.SafeGetString(3);
// this field does not exist but I tried to read it
// this should fail and raise an exception
// it does fail but does not raise the exception
field[4] = dr.SafeGetString(4);
}
}
// this catch is ignored
catch (MySqlException ex)
{ some code }
// the finally block executes okay
finally { some clean up}
// and this return statement is ignored and the code in the caller does not execute
return table1
}
谢谢你的帮助。
catch (MySqlException ex)
{ some code }
catch (Exception ex)
{
//Unexpected exception thrown...
}