如果我对 try/catch block 中的错误的响应是将用户重定向到错误页面,则 try/catch block 的行为就好像有错误,而实际上没有。如果我将其更改为执行其他操作,则代码可以正常工作。
例子:
try
{
//do this SQL server stuff
}
catch
{
Response.Redirect(error.htm)
//Change this to lblErr.Text = "SQL ERROR"; and the code in try works fine.
}
从另一篇文章中我了解到 Response.Redirect() 方法有一个 bool 重载。我尝试了 true 和 false,try/catch block 仍然表现得好像有错误。
这是怎么回事?
最佳答案
当您使用 Response.Redirect 时,会抛出 ThreadAbortException。因此,为了获得您所描述的结果,您需要按如下方式修改代码:
try
{
// Do some cool stuff that might break
}
catch(ThreadAbortException)
{
}
catch(Exception e)
{
// Catch other exceptions
Response.Redirect("~/myErrorPage.aspx");
}
关于c# - 在 Try/Catch block 中使用 Response.Redirect(url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12358535/