title: C# exception handling in sharepoint know for sure file is not found
body: <p>This is my Code:</p>
  
  <pre><code>   try{
         using (Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(siteURL))
              {
                  #region get the name of the file and check if the  file is valid
  
                  context.AuthenticationMode = Microsoft.SharePoint.Client.ClientAuthenticationMode.FormsAuthentication;
                  Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo formsAuthInfo = new
                  Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo(UserName, Password);
                  context.FormsAuthenticationLoginInfo = formsAuthInfo;
                  File file = context.Web.GetFileByServerRelativeUrl(relativeFilePath);
                  context.Load(file);
                  context.ExecuteQuery();
                  documentName = Convert.ToString(file.Name);                    
                  #endregion
  
      }
      }
      catch(ServerUnauthorizedAccessexception ex)
      {
  
      }
      catch(WebException We)
      {
  
      }
  
      catch (ServerException s)
      {
  
  if (s.Message == "File Not Found.")
  {
      htmlClose = "&lt;html&gt;&lt;title&gt;Cannot Get File&lt;/title&gt;&lt;body&gt;&lt;script type='text/javascript'&gt;alert('The specified file is not found in Sharepoint.');self.close();&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;";
  }
  else
  {
      htmlClose = "&lt;html&gt;&lt;title&gt;Cannot Get File&lt;/title&gt;&lt;body&gt;&lt;script type='text/javascript'&gt;alert('Unable to retrieve file.Please contact your administrator');self.close();&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;";
  }
  httpContext.Response.Write(htmlClose);
  httpContext.Response.Flush();
      }
  </code></pre>
  
  <p>I want to know if what I have done for 
  making sure file not found in sharepoint is correct.</p>
  
  <p>Basically I have used exception message to validate if file is not found in sharepoint.
  The code where the exception will get thrown is:</p>
  
  <pre><code>    context.Load(file);
      context.ExecuteQuery();
  </code></pre>
  
  <p>I have used different catch blocks for catching:ServerUnauthorizedAccessexception,WebException and server exception.
  I found that Server exception is the one used to make sure file is not found in sharepoint.
  In that part of the code I have done</p>
  
  <pre><code>   catch (ServerException s)
     {
  
     if (s.Message == "File Not Found.")
     {
     htmlClose = "&lt;html&gt;&lt;title&gt;Cannot Get File&lt;/title&gt;&lt;body&gt;&lt;script type='text/javascript'&gt;alert('The specified file is not found in Sharepoint.');self.close();&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;";
     }
     else
     {
     htmlClose = "&lt;html&gt;&lt;title&gt;Cannot Get File&lt;/title&gt;&lt;body&gt;&lt;script type='text/javascript'&gt;alert('Unable to retrieve file.Please contact your administrator');self.close();&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;";
     }
     httpContext.Response.Write(htmlClose);
     httpContext.Response.Flush();
     }
  </code></pre>