title: c# - is it ok to embed a "try/catch" within a "using" statement for a web request? Is my code correct?
body: <p>Is it ok to embed a "try/catch" within a "using" statement for a web request?  Is my code correct?  That is my requirements are:</p>
  
  <ol>
  <li><p>Want to use the "using" statement to make sure resources are released in any case for HttpWebResponse</p>
  
  <ul>
  <li>But still want to do some custom stuff if there is an exception re HttpWebResponse and "response = (HttpWebResponse)request.GetResponse();" in particular.</li>
  </ul></li>
  </ol>
  
  <p>My source code:</p>
  
  <pre><code>        var result = new HttpHeaderInfo();
          HttpWebRequest request = null;
          HttpWebResponse response = null;
          using (response)
          {
              try
              {
                  request = (HttpWebRequest)WebRequest.Create(uri);
                  request.Method = "HEAD";
                  request.KeepAlive = false;
                  request.Timeout = Properties.Settings.Default.WebTimeoutDefault;
  
                  response = (HttpWebResponse)request.GetResponse();
                  result.LastModified = response.LastModified;
                  result.ContentType = response.ContentType;
                  result.StatusCode = response.StatusCode;
                  result.ContentLength = response.ContentLength;
              }
              catch (Exception ex)
              {
                  if (ex is InvalidOperationException ||
                      ex is ProtocolViolationException ||
                      ex is WebException)
                  {
                      result.HttpError = ex;
                      result.LastModified = System.DateTime.MinValue;
                      result.ContentType = null;
                  }
                  else { throw; }
              }
  
          }
  </code></pre>
  
  <p>thanks</p>