1.异常处理的形式
1)即时处理:发现异常随即处理并恢复程序
2)抛出异常:捕获异常后,重新封装异常信息或者直接抛给调用者
示例:
//账号密码有误
string connStr = "server = localhost; database = MyPhoneList;uid = 123; pwd = 1234";
SqlConnection sqlConnection = new SqlConnection(connStr);
string sqlStr = "select * from Student";
SqlCommand sqlCommand = new SqlCommand(sqlStr, sqlConnection);
try
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); ;
}
sqlConnection.Close();
Console.ReadLine();