【问题标题】:Try Catch and then continue尝试 Catch 然后继续
【发布时间】:2018-02-20 07:12:48
【问题描述】:

我有一些代码进入 for 循环,然后使用另一个函数发送电子邮件。

当出现错误时,它不会在出现问题的电子邮件之后继续发送电子邮件。

我希望能发现错误并让系统继续发送其余的电子邮件。请看下面的代码。

public static SendEmailResult setEmailAndSend(DataRow[] rows)
{
    try
    {
        SendEmailResult results = new SendEmailResult();

        foreach (DataRow row in rows)
        {
            try
            {
                //Set Values here
                string strFN = row["FirstName"].ToString();
                string strLN = row["LastName"].ToString();
                string strEmail = row["Email"].ToString();
                //some more here


                 Sent = EmailFunctions.SendEmail(strEmail, strFromAddress, strFromName, strSubject, strFN + " " + strLN, strBody, strSignOffEmbeddedImagePath, strSignOffEmbeddedImageName, strCCReminderEmail);
             }
            catch (Exception exc)
            {
                //do stuff

                continue;
            }
       }
    }
    catch (Exception exc)
    {
        //do something
    }
}


public static bool SendEmail(String strToAddress, String strFromAddress, String strFromName, String strSubject, String strRecipientName, String strBody, String strEmbeddedImagePath, String strEmbeddedImageName, String strCCReminderEmail)
{
    try
    {
        //SMTPClient settings in webconfig
        SmtpClient client = new SmtpClient();
        client.EnableSsl = true;

        using (MailMessage message = new MailMessage(
            new MailAddress(strFromAddress, strFromName),
            new MailAddress(strToAddress, strRecipientName)))
        {
            message.IsBodyHtml = true;
            message.Subject = strSubject;
            message.Body = strBody;

            if (!string.IsNullOrEmpty(strCCReminderEmail))
                message.CC.Add(strCCReminderEmail);

            client.Send(message);

            return true;
        }
    }
    catch (Exception ex)
    {
        throw;

    }
}

【问题讨论】:

  • 为什么我被否决了。

标签: asp.net exception try-catch


【解决方案1】:

您需要在 foreach 语句中移动 try catch 块:

foreach (DataRow row in rows)
     {
                    try
                    {
                        //Set Values here
                        string strFN = row["FirstName"].ToString();
                        string strLN = row["LastName"].ToString();
                        string strEmail = row["Email"].ToString();
                        //some more here


                        Sent = EmailFunctions.SendEmail(strEmail, strFromAddress, strFromName, strSubject, strFN + " " + strLN, strBody, strSignOffEmbeddedImagePath, strSignOffEmbeddedImageName, strCCReminderEmail);
                    }
                    catch (Exception exc)
                    {
                        //Log exception!!
                        continue;
                    }
                }

【讨论】:

  • 抱歉,我忘了在其中添加,我已将其添加到我的示例中,但仍有问题。
  • 您遇到了什么问题?请在此处提供更多详细信息。
【解决方案2】:

您应该try catch 只在那些您认为即使在正常使用中也可能出错的区域,如互联网、IO、流等。

在你的情况下,SendEmail(...) 是,所以

try
{
    Sent = EmailFunctions.SendEmail(...);
} 
catch (Exception ex)
{
    // Capture mail send exception
}

或者只是

public static bool SendEmail(...)
{
    try
    {
        ...
        using (MailMessage message = new MailMessage(...))
        {
           ...

            return true;
        }
    }
    catch (Exception ex)
    {        
        // Some function to record which mail didn't send
        return false;
    }
}

因为throw 在这里似乎没有必要。

如果您也想捕获数据库访问问题,您可以将try catch 保留在foreach 之外(但删除continue,它在循环之外没有意义)。

【讨论】:

  • 到底是什么问题?任何异常消息?
猜你喜欢
  • 1970-01-01
  • 2020-12-24
  • 2010-11-23
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 2019-06-19
相关资源
最近更新 更多