尝试抓取,然后继续

时间:2017-09-12 00:01:28

标签: asp.net exception try-catch

我有一些代码进入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;

    }
}

2 个答案:

答案 0 :(得分: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;
                    }
                }

答案 1 :(得分:0)

您应该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,它在循环之外没有意义。)