datareader 失败但不引发异常 c#
datareader fails but does not raise exception c#
我是 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...
}
我是 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...
}