title: How to handle 404 not found exception when downloading a file?
body: <p>This is the method i'm using to download the file.
  My program is using a timer and every 15 minutes it's trying to download the file:</p>
  
  <pre><code>HttpWebRequest request;
          int currentIndex = 0;
          void fileDownloadRadar(string uri, string fileName)
          {
              if (splash != null)
              {
                  if (!splash.IsDisposed)
                      splash.UpdateProgressBar(0);
              }
              try
              {
                  request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
                  request.ContentType = "text/html";
                  request.CookieContainer = new CookieContainer();
                  request.AllowAutoRedirect = true;
                  request.Timeout = 10000;
                  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                  {
  
                      long contentLength = response.ContentLength;
                      if (response.ContentType == "")
                      {
                          Logger.Write("ContentType is Empty download was not fine !!!!!");
                      }
                      if ((response.StatusCode == HttpStatusCode.OK ||
                          response.StatusCode == HttpStatusCode.Moved ||
                          response.StatusCode == HttpStatusCode.Redirect) &amp;&amp;
                          response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
                      {
                          Logger.Write("ContentType is not empty meaning download is fine");
                          using (Stream inputStream = response.GetResponseStream())
                          using (Stream outputStream = File.OpenWrite(fileName))
                          {
                              inputStream.ReadTimeout = 10000;
                              inputStream.WriteTimeout = 10000;
                              byte[] buffer = new byte[4096];
                              int bytesRead;
                              do
                              {
                                  bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                                  currentIndex += bytesRead;
                                  double percentage = (double)currentIndex / contentLength;
                                  if (splash != null)
                                  {
                                      if (!splash.IsDisposed)
                                          splash.UpdateProgressBar((int)(percentage * 100));
                                  }
                                  outputStream.Write(buffer, 0, bytesRead);
                              } while (bytesRead != 0);
                              if (splash != null)
                              {
                                  if (!splash.IsDisposed)
                                  {
                                      splash.UpdateProgressBar(100);
                                  }
                              }
                          }
  
                      }
                      else
                      {
                          timer1.Stop();
                          timer3.Start();
                      }
                      if (splash == null)
                          FinishWebRequest();
                  }
              }
              catch (Exception ex)
              {                
                    Logger.Write(ex.ToString());
              }
          }
  </code></pre>
  
  <p>The program was running fine for few hours and then about 30 minutes ago it throw this exception on the line:</p>
  
  <pre><code>using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  </code></pre>
  
  <p>WebException: The remote server returned an error: (404) Not Found</p>
  
  <p>The full exception message:</p>
  
  <pre><code>System.Net.WebException occurred
    HResult=-2146233079
    Message=The remote server returned an error: (404) Not Found.
    Source=System
    StackTrace:
         at System.Net.HttpWebRequest.GetResponse()
         at mws.Form1.fileDownloadRadar(String uri, String fileName) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 1015
    InnerException:
  </code></pre>
  
  <p>Line 1015 is:</p>
  
  <pre><code>using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  </code></pre>
  
  <p>It didn't jump to the catch part just throw the exception on this line.
  My question how should i handle this case ? </p>
  
  <p>Maybe, i thought to do in case of this exception  to call a method that will try to download the file like 15 times each time 30 seconds try and if after 15 times it didn't download then continue to the next 15 minutes. But i'm not sure if this is a good solution and in any case how should i handle the exception ? I added try and catch but it didn't get to the catch.</p>