title: FileStream in a try and catch, how critical is it to close the reader?
body: <p>I want to make sure to close the reader after creating a reader when there is an exception. So I want to include a finally bracket to this try and catch. But I can't close it in the finally bracket unless I have the reader declared outside above the try bracket. But if I do that I won't be catching any exceptions when creating the reader. I am not sure how critical it is to close the reader or not.</p>
  
  <pre><code>XmlSerializer xmlSr = new XmlSerializer(typeof(List&lt;ProjectObject&gt;));
  try
  {
      FileStream reader = new FileStream(mTextFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
      List&lt;ProjectObject&gt; addProjects = (List&lt;ProjectObject&gt;)xmlSr.Deserialize(reader);
      mSharedDriveLocalProjects = addProjects;
      reader.Close();
  }
  catch
  {
      MessageBox.Show("Failed to load XML file");
  }
  </code></pre>
  
  <p>The above is my code. Is it better to have it this way?</p>
  
  <pre><code>XmlSerializer xmlSr = new XmlSerializer(typeof(List&lt;ProjectObject&gt;));
  FileStream reader = new FileStream(mTextFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  try
  {
      List&lt;ProjectObject&gt; addProjects = (List&lt;ProjectObject&gt;)xmlSr.Deserialize(reader);
      mSharedDriveLocalProjects = addProjects;
  }
  catch
  {
      MessageBox.Show("Failed to load XML file");
  }
  finally
  {
      reader.Close();
  }
  </code></pre>