如果我有很多异常要处理,那么 C# 异常处理的最佳方法是什么?
我会将它们全部放在 try 块中还是尽可能多地放入 try 块?
例如,当我按如下方式连接到数据库时,
放置 try...catch...finally 块的最佳方法是什么?
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=school.mdb");
conn.Open();
string sql = "select * from sheet1 where student='stu2'";
OleDbCommand command;
command = new OleDbCommand(sql, conn);
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write("{0} ", reader[i]);
}
Console.WriteLine();
}
reader.Close();
conn.Close();