title: How to properly catch a 404 error in .NET
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1949610/c-how-can-i-catch-a-404">How can I catch a 404?</a>  </p>
  </blockquote>
  
  
  
  <p>I would like to know the proper way to catch a 404 error with c# asp.net here is the code I'm using</p>
  
  <pre><code>HttpWebRequest request = (HttpWebRequest) WebRequest.Create(String.Format("http://www.gravatar.com/avatar/{0}?d=404", hashe));
  
  // execute the request
  try
  {
      //TODO: test for good connectivity first
      //So it will not update the whole database with bad avatars
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      Response.Write("has avatar");
  }
  catch (Exception ex)
  {
      if (ex.ToString().Contains("404"))
      {
           Response.Write("No avatar");
      }
  }
  </code></pre>
  
  <p>This code works but I just would like to know if this is the most efficient.</p>