title: Continue after HttpWebRequest response returns 404 error
body: <p>I need to determine if a list of users are in a 3rd party user directory.  I am writing the method in C#.NET 4.0. The user list is over 2700 entries. The API to access this user directory uses REST to return the user information.  If the user is not in the directory, response to the HttpWebRequest is a 404 error. I catch the WebException error.  </p>
  
  <p>The problem is I can only get the user information for 1 user at a time so I would like to do a for loop to go thru the user list.  However, when the 404 error occurs, it breaks out of the loop.  Is there any way to catch the 404 error and continue the loop with the next user?</p>
  
  <p>This is my code:</p>
  
  <pre><code>    public void GetRecipientDataFromCortext()
  {
      SqlConnection SqlConn = null;
      CortextUser objCortextResult;
      try
      {
          string strStoredProcedure = "PagingToolGetRecipientsAndDevices";
          DataTable tdtRecipientData = new DataTable("RecipientData");
          //Create a datatable to store final results
          DataTable tdtCortextResultData = new DataTable("CortextResults");
          tdtCortextResultData.Columns.Add("RecipientID", typeof(int));
          tdtCortextResultData.Columns.Add("DeviceTypeID", typeof(int));
          tdtCortextResultData.Columns.Add("DeviceAddress", typeof(string));
          tdtCortextResultData.Columns.Add("CUID", typeof(string));
          tdtCortextResultData.Columns.Add("InviteStatus", typeof(string));
          tdtCortextResultData.Columns.Add("IsEnabled", typeof(bool));
          tdtCortextResultData.Columns.Add("FirstName", typeof(string));
          tdtCortextResultData.Columns.Add("LastName", typeof(string));
          tdtCortextResultData.Columns.Add("MiddleName", typeof(string));
          tdtCortextResultData.Columns.Add("HonorificPrefix", typeof(string));
          tdtCortextResultData.Columns.Add("HonorificSuffix", typeof(string));
          tdtCortextResultData.Columns.Add("Email", typeof(string));
          tdtCortextResultData.Columns.Add("Mobile", typeof(string));
          tdtCortextResultData.Columns.Add("Pager", typeof(string));
  
          string strSqlConnection = ConfigurationManager.ConnectionStrings[CONNECTION_STRING].ConnectionString;
          //Get the recipient list from the database
          using (SqlConn = new SqlConnection(strSqlConnection))
          {
              using (SqlCommand SqlCmd = new SqlCommand(strStoredProcedure, SqlConn))
              {
                  SqlCmd.CommandType = CommandType.StoredProcedure;
                  SqlConn.Open();
                  using (SqlDataAdapter dataReturned = new SqlDataAdapter(SqlCmd))
                  {
                      dataReturned.Fill(tdtRecipientData);                         
                  }
              }
          }
          //For loop to make a call to cortext to get properties of this device 
          string tstrPropertyValue = string.Empty;
          string tstrProperty = string.Empty;
          int tiDeviceTypeID;
          int tiRecipientID;
  
          //This is the for loop to check each recipient to see if he/she is in the directory
          foreach (DataRow row in tdtRecipientData.Rows)
          {               
              tstrPropertyValue = row["Address"].ToString();
              tiDeviceTypeID = Convert.ToInt32(row["DeviceTypeID"].ToString());
              tiRecipientID = Convert.ToInt32(row["RecipientID"].ToString());
  
              //Insert into the Cortext table initial values first
              //This inserts into a database table the user to check.
              //This table is used to get the initial recipient list. 
              //Any users in this table are not in the list.
              InsertCortextInformation(tiRecipientID, tiDeviceTypeID, tstrPropertyValue);
  
              switch (tiDeviceTypeID)
              {
                  case 1:
                      tstrProperty = "mobile";
                      break;
                  case 2:
                      tstrProperty = "email";
                      break;
                  case 3:
                      tstrProperty = "pager";
                      break;
                  default:
                      break;
              }
  
              //Results from cortext call add to data table
              //User not in the directory results in this method call to return a 404 error.
              //Exception breaks out of the loop
              objCortextResult = GetCortextUserInformation(tstrProperty, tstrPropertyValue);
              //Add results to datatable
              DataRow newRow = tdtCortextResultData.NewRow();
              newRow["RecipientID"] = Convert.ToInt32(row["RecipientID"].ToString());
              newRow["DeviceTypeID"] = Convert.ToInt32(row["DeviceTypeID"].ToString());
              newRow["DeviceAddress"] = row["Address"].ToString();
              newRow["CUID"] = objCortextResult.CUID;
              newRow["InviteStatus"] = objCortextResult.inviteStatus;
              newRow["IsEnabled"] = objCortextResult.isEnabled;
              newRow["FirstName"] = objCortextResult.userFirstName;
              newRow["LastName"] = objCortextResult.userLastName;
              newRow["MiddleName"] = objCortextResult.userMiddleName;
              newRow["HonorificPrefix"] = objCortextResult.userHonorificPrefix;
              newRow["HonorificSuffix"] = objCortextResult.userHonorificSuffix;
              newRow["Email"] = objCortextResult.email;
              newRow["Mobile"] = objCortextResult.mobile;
              newRow["Pager"] = objCortextResult.pager;
  
              tdtCortextResultData.Rows.Add(newRow);
  
          }
  
          //Insert data table results into Paging Tool table, PagingToolCortextInfo
          int tiRecID, tiDeviceID;
          bool tbIsEnabled;
          foreach (DataRow row in tdtCortextResultData.Rows)
          {
              tiRecID = Convert.ToInt32(row["RecipientID"].ToString());
              tiDeviceID = Convert.ToInt32(row["DeviceTypeID"].ToString());
              tbIsEnabled = Convert.ToBoolean(row["IsEnabled"].ToString());
              UpdateCortextInformation(tiRecID, tiDeviceID, row["DeviceAddress"].ToString(),
                                          row["CUID"].ToString(), row["InviteStatus"].ToString(), tbIsEnabled,
                                          row["FirstName"].ToString(), row["LastName"].ToString(), row["MiddleName"].ToString(),
                                          row["HonorificPrefix"].ToString(), row["HonorificSuffix"].ToString(),
                                          row["Email"].ToString(), row["Mobile"].ToString(), row["Pager"].ToString());
          }
  
      }
      //Catches the HttpWebResponse errors like 404
      catch (WebException e)
      {
          using (WebResponse response = e.Response)
          {
              HttpWebResponse httpResponse = (HttpWebResponse)response;
              string errorMessage = string.Format("Error code: {0} ", httpResponse.StatusCode);
              //         System.Diagnostics.Debug.WriteLine("Error code: {0}", httpResponse.StatusCode);
              using (Stream data = response.GetResponseStream())
              {
                  using (var reader = new StreamReader(data))
                  {
                      string text = reader.ReadToEnd();
                      errorMessage += text;
                      //
                      System.Diagnostics.Debug.WriteLine(text);
                      logger.ErrorException(errorMessage, e);
                  }
              }
          }
          throw;
      }
      catch (Exception ex)
      {
          logger.ErrorException(ex.Message, ex);
          throw;
      }
      finally
      {
          if (SqlConn != null)
          {
              SqlConn.Close();
          }
      }
  }
  </code></pre>
  
  <p>So, is there any way to continue in the for loop after the call Http response returns a 404 error and get the next user?</p>
  
  <p>I would like to acknowledge the 404 error and continue in the for loop to get all of the users.</p>
  
  <p>Thanks.</p>