C#C
C#16mo ago
Merineth

✅ User defined Exception Handling

namespace Workshop3.Menus
{
    public class ReadTextFileMenu : Menu
    {
        public ReadTextFileMenu(string name)
            : base(name)
        {
            Add(new MenuItem("Read Text File", new Command<ReadTextFileMenu>(this, r => r.ReadTextFile())));
        }

        public void ReadTextFile()
        {
            // Clear the console from previous text
            Console.Clear();
            // Asking the user for filepath
            Console.WriteLine("Enter filepath..");
            // Saving the filepath as a string object
            string filepath = Console.ReadLine();
            if (!File.Exists(filepath))
            {
                throw new FileDoesNotExistException(filepath);
            }

            TextFileReader.StreamTextFile(filepath);




        }
    }
}

namespace Workshop3.Exceptions
{
    public class FileDoesNotExistException : Exception
    {
        public FileDoesNotExistException(string filepath) : base($"The filepath {filepath} does not exist! Press Enter to try again. ")
        {
            Console.ReadLine();
            ReadTextFileMenu.ReadTextFile();
        }
    }
}


Why do i get a
"An object reference is required for the non-static field, method, or property 'ReadTextFileMenu.ReadTextFile()'
error?
Was this page helpful?