title: DRY With Different Try Statements and Identical Catch Statements
body: <p>So I have the following block of code inside a method: (all variables are local)</p>
  
  <pre><code>// ...
  
  try
  {
      if (postXml != null)
          using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
              writer.Write(postXml.ToString());
  }
  catch (WebException ex)
  {
      HttpWebResponse response = ex.Response as HttpWebResponse;
      if (response != null)
          result = HandleOtherResponse(response, out status);
      else result = HandleBadResponse(ex.ToString(), out status);
  }
  catch (Exception ex)
  {
      result = HandleBadResponse(ex.ToString(), out status);
  }
  
  if (result == null)
  {
      try
      {
          HttpWebResponse response = req.GetResponse() as HttpWebResponse;
          result = HandleOtherResponse(response, out status);
      }
      catch (WebException ex)
      {
          HttpWebResponse response = ex.Response as HttpWebResponse;
          if (response != null)
              result = HandleOtherResponse(response, out status);
          else result = HandleBadResponse(ex.ToString(), out status);
      }
      catch (Exception ex)
      {
          result = HandleBadResponse(ex.ToString(), out status);
      }
  }
  
  // ...
  </code></pre>
  
  <p>As you can see, the two try statements are different, but the two sets of catch statements are exactly the same. I've been trying to think of a way that it might be possible to not repeat myself here, but I haven't really thought of a way that wouldn't be significantly slower or just as terrible looking. Wondering if anyone has any ideas.</p>