Please help me with the task: Finally finally

Hello, I’m working on this task:

To the existent try-catch construction, add a finally block, in which you release all system resources that Noname’s reader used. Use a method CleanUpAllResources for this.

Here is my code:

using System.IO;

namespace Exceptions
{
   public class FinallyFinally
   {
       public static void Main()
       {
           NonamesFileReader fileReader = new NonamesFileReader();

           try
           {
               fileReader.OpenFile("D:/someFileFromTheServer.txt");
               var lineFromFile = fileReader.ReadLine();
               Console.WriteLine(lineFromFile);
           }
           catch (IOException e)
           {
               Console.WriteLine($"Error reading from someFileFromTheServer. Message = {e.Message}");
           }
           finally
           {
               NonamesFileReader.CleanUpAllResources();
           }
       }
   }

   class NonamesFileReader
   {
       StreamReader streamReader;

       public void OpenFile(string fileName)
       {
           streamReader = new StreamReader(fileName);
       }

       public string ReadLine()
       {
           return streamReader.ReadLine();
       }

       public void CleanUpAllResources()
       {
           if (streamReader != null)
           {
               streamReader.Close();
           }
           Console.WriteLine("Resources deallocated.");
       }
   }
}

Can anyone please help me to solve it?

The object name on which you need to call CleanUpAllResources is not NonamesFileReader but a fileReader. Everything else looks fine! :+1:

1 Like

Thanks a lot. Small mistake from my side!

1 Like