Try/catch function only runs once even with a while loop C#

Viewed 191

I am trying to create a command-line application that searches through a directory of song names and albums input by the user and then plays that song. When the user inputs a non-existing song name or album, the program throws a DriectoryNotFoundException.

I have put the "song grabber" function within the try block and then if a user inputs an invalid directory, it goes to the catch block and tries again. It works as intended, yet only once. I have tried putting a while loop to make sure the try/catch function keeps working until the program fully succeeds.

while (true)
{
    try
    {
        MusicSelector.SongGrabber();
        break;
    }
    catch (System.IO.DirectoryNotFoundException)
    {
        Console.WriteLine("Enter the exact song names and album.");
        MusicSelector.SongGrabber();
    }
}

What should I do in order to retry until the user inputs a valid directory?

3 Answers

If the user does it wrong the second time, the call of MusicSelector.SongGrabber() in the catch block will throw another exception which isn't catched and ends the program.

So don't call MusicSelector.SongGrabber() in the catch block again. Do nothing in the catch block, or even better print a message to inform the user of what went wrong. The loop will be iterated again and the call of MusicSelector.SongGrabber() will be done again in the try block so that the catch block can catch a new exception should one be thrown.

And it would certainly be nice to give the user the instructions before, so that they know what they're supposed to do. I.e. you may also want to consider to move the Console.WriteLine().

while (true)
{
    try
    {
        Console.WriteLine("Enter the exact song names and album.");
        MusicSelector.SongGrabber();
        break;
    }
    catch (System.IO.DirectoryNotFoundException)
    {
        Console.WriteLine("The directory wasn't found.");
    }
}

And try-catch isn't a function but a statement.

You break out of the loop after a successful invocation of SongGrabber. Remove that break and the call from the catch (since you have one in the try block and the loop will ensure you revisit it) and you should be OK:

while (true)
{
    try
    {
        MusicSelector.SongGrabber();
        // break removed here
    }
    catch (System.IO.DirectoryNotFoundException)
    {
        Console.WriteLine("Enter the exact song names and album.");
    }
} 

If I'm reading your description, I see that you are trying to fix something with a workaround(try-catch) instead of actually tackling the issue.

You know that the user can fill in an incorrect path so you need to check on that and then start the search if it's correct or stop the code and give the user feedback that the path is incorrect.

I'm not exactly sure how you build your code but here is how I would do it.

Console.WriteLine("Enter the exact song names and album.");
var path = path.Trim(); //This is the parameter of the user sending the path

//If directory does not exist, don't even try. Give feedback and stop
if (!Directory.Exists(path))  
{  
    //In case they cannot start it again then call the method on how to ask the path in here
    Console.WriteLine("Path is invalid, try again");
    return;
}

//try to get the song
var song = MusicSelector.SongGrabber(); //I assume you should pass the path here as a parameter.

//No song was found
if(song == null)
{
   Console.WriteLine("There was no song found");
   return;
}

//Song was found. Do something with it like play the song :)
MusicPlayer.Play(song);
                 
            
Related