简体   繁体   English

即使try块未抛出异常,捕获块仍在执行?

[英]Catch block executing even when try block throws no exception?

I am trying to send a mail via goDaddy mail server. 我正在尝试通过goDaddy邮件服务器发送邮件。 The mail is being sent (I am receiving it in my mailbox) but the catch block is executing and landing me to the contact page with querystring msg as "fail" 邮件正在发送(我已经在我的邮箱中接收到了邮件),但是catch块正在执行并且将我带到查询字符串msg为“ fail”的联系页面

    protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["username"] != null && Request.QueryString["email"] != null && Request.QueryString["msg"] != null)
    {
        try
        {
            string name = Request.QueryString[0];
            string email = Request.QueryString[1];
            string msg = Request.QueryString[2];
            SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25);
            //smtp.EnableSsl = false;
            smtp.Send("feedback@solutionpoint98.com", "b.soham1991@gmail.com", "feedback", "Name:" + name + "\n e-mail:" + email + "\n Subject:" + msg);
            Response.Redirect("contact.html?msg=success");
        }
        catch(Exception ex)
        {
            Response.Redirect("contact.html?msg=fail");
        }
    }
    else
    {
        Response.Redirect("contact.html");
    }
}

If the mail is being sent, shouldn't it redirect to "contact.html?msg=success"? 如果正在发送邮件,是否应该将其重定向到“ contact.html?msg =成功”?

The problem is that your first Response.Redirect() is actually throwing a ThreadAbortException. 问题是您的第一个Response.Redirect()实际上抛出了ThreadAbortException。 This is due to the fact that .Redirect internally calls the Response.End method, prematurely ending the Response in this case. 这是由于以下事实:.Redirect在内部调用Response.End方法,在这种情况下过早地结束了Response。

The correct way to do this is to use the overloaded Response.Redirect(string url, bool endResponse), which allows you to suppress the internal call to Response.End by setting endResponse to false ... which will prevent this error from being thrown. 正确的方法是使用重载的Response.Redirect(string url,bool endResponse),该方法允许您通过将endResponse设置为false来抑制对Response.End的内部调用,这将防止抛出此错误。

This is all covered in this MS support article: http://support.microsoft.com/kb/312629/EN-US/ 此MS支持文章全面介绍了这些内容: http : //support.microsoft.com/kb/312629/EN-US/

Just modify your code to look like this: 只需将代码修改为如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["username"] != null && Request.QueryString["email"] != null && Request.QueryString["msg"] != null)
    {
        try
        {
            string name = Request.QueryString[0];
            string email = Request.QueryString[1];
            string msg = Request.QueryString[2];
            SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25);
            //smtp.EnableSsl = false;
            smtp.Send("feedback@solutionpoint98.com", "b.soham1991@gmail.com", "feedback", "Name:" + name + "\n e-mail:" + email + "\n Subject:" + msg);
            Response.Redirect("contact.html?msg=success", false);
        }
        catch(Exception ex)
        {
            Response.Redirect("contact.html?msg=fail", false);
        }
    }
    else
    {
        Response.Redirect("contact.html", false);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM