ASP.Net C#-尝试catch finally并使用语句
因此,我已经习惯于使用try-catch-finally语句进行编码,而不包括using语句,我正在尝试将后者合并到我的代码中 我在下面附上了我的原始代码和修订代码。这个修订是否足够 另外,关于错误捕获,我已经在这里看到以下代码被多次使用。什么时候应该使用/不使用,因为这不会通知用户错误ASP.Net C#-尝试catch finally并使用语句,c#,asp.net,error-handling,try-catch,using,C#,Asp.net,Error Handling,Try Catch,Using,因此,我已经习惯于使用try-catch-finally语句进行编码,而不包括using语句,我正在尝试将后者合并到我的代码中 我在下面附上了我的原始代码和修订代码。这个修订是否足够 另外,关于错误捕获,我已经在这里看到以下代码被多次使用。什么时候应该使用/不使用,因为这不会通知用户错误 catch (Exception ex) { throw ex; } 原始代码: protected void signIn() { string connStr = Configuratio
catch (Exception ex)
{
throw ex;
}
原始代码:
protected void signIn()
{
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand comm;
comm = new MySqlCommand("Select user_id, username, email, salt, hashed_pw, role, activated FROM users WHERE username=@username", conn);
comm.Parameters.Add("@username", MySqlDbType.VarChar);
comm.Parameters["@username"].Value = txtUsername.Text;
MySqlDataReader reader;
try
{
conn.Open();
reader = comm.ExecuteReader();
if (reader.Read())
{
string saltAndPwd = String.Concat(txtPassword.Text, reader["salt"].ToString());
string hashSaltAndPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
if (hashSaltAndPwd.Equals(reader["hashed_pw"].ToString()))
{
if (reader["activated"].ToString().Equals("Y"))
{
Session["Username"] = reader["username"].ToString();
Session["Role"] = reader["role"].ToString();
Session["UserID"] = reader["user_id"].ToString();
Session["EmailAddress"] = reader["email"].ToString();
if (reader["role"].ToString().Equals("0"))
{
Session["PermanentRole"] = "admin";
}
else if (reader["role"].ToString().Equals("2"))
{
Session["PermanentRole"] = "tutor";
}
Response.Redirect("~/portal.aspx");
}
else
{
lblError.Text = "Your account has not been activated. Please check your inbox and activate your account or reset your password by clicking the link above.";
}
}
else
{
lblError.Text = "Incorrect password.";
}
}
else
{
lblError.Text = "Username does not exist.";
}
reader.Close();
}
catch
{
lblError.Text = "Database connection error. Please try again.";
}
finally
{
conn.Close();
}
}
修订守则:
protected void signIn()
{
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (MySqlConnection conn = new MySqlConnection(connStr))
{
using (MySqlCommand cmd = conn.CreateCommand())
{
string cmdText = "Select user_id, username, email, salt, hashed_pw, role, activated FROM users WHERE username=@username";
cmd.CommandText = cmdText;
cmd.Parameters.Add("@username", MySqlDbType.VarChar);
cmd.Parameters["@username"].Value = txtUsername.Text;
try
{
conn.Open();
reader = cmd.ExecuteReader();
if (reader.Read())
{
string saltAndPwd = String.Concat(txtPassword.Text, reader["salt"].ToString());
string hashSaltAndPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
if (hashSaltAndPwd.Equals(reader["hashed_pw"].ToString()))
{
if (reader["activated"].ToString().Equals("Y"))
{
Session["Username"] = reader["username"].ToString();
Session["Role"] = reader["role"].ToString();
Session["UserID"] = reader["user_id"].ToString();
Session["EmailAddress"] = reader["email"].ToString();
if (reader["role"].ToString().Equals("0"))
{
Session["PermanentRole"] = "admin";
}
else if (reader["role"].ToString().Equals("2"))
{
Session["PermanentRole"] = "tutor";
}
Response.Redirect("~/portal.aspx");
}
else
{
lblError.Text = "Your account has not been activated. Please check your inbox and activate your account or reset your password by clicking the link above.";
}
}
else
{
lblError.Text = "Incorrect password.";
}
}
else
{
lblError.Text = "Username does not exist.";
}
reader.Close();
}
catch
{
lblError.Text = "Database connection error. Please try again.";
}
finally
{
conn.Close();
}
}
}
您不需要
finally{…}
,因为使用
将处理连接
你的问题是:
catch (Exception ex)
{
throw ex;
}
当您想记录异常但仍抛出异常时,这种情况很常见,简单地捕获并重新抛出异常是没有用的
但应该这样做:
catch (Exception ex)
{
LogManager.Log(ex);
throw; //rethrow
}
您不需要
finally{…}
,因为使用
将处理连接
你的问题是:
catch (Exception ex)
{
throw ex;
}
当您想记录异常但仍抛出异常时,这种情况很常见,简单地捕获并重新抛出异常是没有用的
但应该这样做:
catch (Exception ex)
{
LogManager.Log(ex);
throw; //rethrow
}
您不需要
finally{…}
,因为使用
将处理连接
你的问题是:
catch (Exception ex)
{
throw ex;
}
当您想记录异常但仍抛出异常时,这种情况很常见,简单地捕获并重新抛出异常是没有用的
但应该这样做:
catch (Exception ex)
{
LogManager.Log(ex);
throw; //rethrow
}
您不需要
finally{…}
,因为使用
将处理连接
你的问题是:
catch (Exception ex)
{
throw ex;
}
当您想记录异常但仍抛出异常时,这种情况很常见,简单地捕获并重新抛出异常是没有用的
但应该这样做:
catch (Exception ex)
{
LogManager.Log(ex);
throw; //rethrow
}
1) conn.Close()代码>不是必需的,因为using语句将为您调用close
。相当于
MySqlConnection conn = new MySqlConnection(connStr)
try
{
....
}
finally
{
conn.Close();
}
2) 形式的捕捉
catch (Exception ex)
{
throw ex;
}
在我能想到的任何情况下都不推荐。它有两个问题
- 除了重新引用异常,它什么也不做。除非要对异常执行某些操作(例如:记录错误),否则无法捕获异常
- 重新调用异常do
throw ex代码>剪切堆栈跟踪。任何捕捉到该异常的人都会看到该行生成的错误,从而丢失有用的信息
1)
conn.Close()代码>不是必需的,因为using语句将为您调用close
。相当于
MySqlConnection conn = new MySqlConnection(connStr)
try
{
....
}
finally
{
conn.Close();
}
2) 形式的捕捉
catch (Exception ex)
{
throw ex;
}
在我能想到的任何情况下都不推荐。它有两个问题
- 除了重新引用异常,它什么也不做。除非要对异常执行某些操作(例如:记录错误),否则无法捕获异常
- 重新调用异常do
throw ex代码>剪切堆栈跟踪。任何捕捉到该异常的人都会看到该行生成的错误,从而丢失有用的信息
1)
conn.Close()代码>不是必需的,因为using语句将为您调用close
。相当于
MySqlConnection conn = new MySqlConnection(connStr)
try
{
....
}
finally
{
conn.Close();
}
2) 形式的捕捉
catch (Exception ex)
{
throw ex;
}
在我能想到的任何情况下都不推荐。它有两个问题
- 除了重新引用异常,它什么也不做。除非要对异常执行某些操作(例如:记录错误),否则无法捕获异常
- 重新调用异常do
throw ex代码>剪切堆栈跟踪。任何捕捉到该异常的人都会看到该行生成的错误,从而丢失有用的信息
1)
conn.Close()代码>不是必需的,因为using语句将为您调用close
。相当于
MySqlConnection conn = new MySqlConnection(connStr)
try
{
....
}
finally
{
conn.Close();
}
2) 形式的捕捉
catch (Exception ex)
{
throw ex;
}
在我能想到的任何情况下都不推荐。它有两个问题
- 除了重新引用异常,它什么也不做。除非要对异常执行某些操作(例如:记录错误),否则无法捕获异常
- 重新调用异常do
throw ex代码>剪切堆栈跟踪。任何捕捉到该异常的人都会看到该行生成的错误,从而丢失有用的信息
“我在这里多次看到以下代码被使用。由于这不会通知用户错误,何时应该使用/不使用它?”不,从来没有,你在哪里见过它?不要在一个空的Catch
中执行抛出ex
。读取,您不需要关闭连接,使用的块将为您处理该问题(从技术上讲,是连接类的dispose方法)“什么时候应该使用它?”
-从不<代码>抛出ex代码>不应该这样使用。它正在丢弃有用的堆栈跟踪信息。如果您只需要重新抛出相同的异常,请使用throw代码>。如果您所做的一切都是重新抛出相同的异常,请完全忽略catch
块,因为它什么也不做。“我在这里多次看到下面的代码。什么时候应该使用/不使用它,因为它不会通知用户错误?”不,从来没有,您在哪里看到过它?不要在一个空的Catch
中执行抛出ex
。读取,您不需要关闭连接,使用的块将为您处理该问题(从技术上讲,是连接类的dispose方法)“什么时候应该使用它?”
-从不<代码>抛出ex代码>不应该这样使用。它正在丢弃有用的堆栈跟踪信息。如果您只需要重新抛出相同的异常,请使用throw代码>。如果您所做的一切都是重新抛出相同的异常,请完全忽略catch
块,因为它什么也不做。“我在这里多次看到下面的代码。什么时候应该使用/不使用它,因为它不会通知用户错误?”不,从来没有,您在哪里看到过它?不要做抛出ex