jloga4
7/20/2017 - 3:11 AM

Example of how to use the finally statement in a try construct to ensure some code is executed. From Fundamentals of Computer Programming wi

Example of how to use the finally statement in a try construct to ensure some code is executed. From Fundamentals of Computer Programming with C# http://www.introprogramming.info/wp-content/uploads/2013/07/Books/CSharpEn/Fundamentals-of-Computer-Programming-with-CSharp-Nakov-eBook-v2013.pdf

static void Main()
{
  //string fileName = "WrongTextFile.txt";
  //ReadFile(fileName);
}

static void ReadFile(string fileName)
{
  TextReader reader = new StreamReader(fileName);
  try
  {
    string line = reader.ReadLine();
    Console.WriteLine(line);
  }
  finally
  {
    reader.Close();
  }
}