【问题标题】:Try/catch function only runs once even with a while loop C#即使使用 while 循环 C#,try/catch 函数也只运行一次
【发布时间】:2021-04-04 06:54:52
【问题描述】:

我正在尝试创建一个命令行应用程序,该应用程序搜索用户输入的歌曲名称和专辑目录,然后播放该歌曲。当用户输入一个不存在的歌曲名或专辑时,程序会抛出一个DriectoryNotFoundException

我已将“歌曲抓取器”功能放在 try 块中,然后如果用户输入无效目录,它会转到 catch 块并再次尝试。它按预期工作,但只有一次。我已经尝试放置一个 while 循环来确保 try/catch 函数继续工作,直到程序完全成功。

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

在用户输入有效目录之前,我应该怎么做才能重试?

【问题讨论】:

  • 理想情况下,您不会使用 try catch 作为主要流控制。您将改为使用其中一种 Exist 方法检查目录,然后回退到异常处理

标签: c# loops exception while-loop try-catch


【解决方案1】:

在成功调用SongGrabber 后,您的break 退出了循环。删除 break 和来自 catch 的调用(因为您在 try 块中有一个,并且循环将确保您重新访问它),您应该没问题:

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

【讨论】:

    【解决方案2】:

    如果用户第二次做错了,在catch块中调用MusicSelector.SongGrabber()会抛出另一个未被捕获的异常并结束程序。

    所以不要再在catch 块中调用MusicSelector.SongGrabber()。在catch 块中什么都不做,或者更好地打印一条消息来通知用户出了什么问题。循环将再次迭代,MusicSelector.SongGrabber() 的调用将在 try 块中再次完成,以便 catch 块可以捕获新的异常,以防抛出异常。

    在之前给用户提供说明当然很好,这样他们就知道他们应该做什么。 IE。您可能还想考虑移动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.");
        }
    }
    

    try-catch 不是函数而是语句。

    【讨论】:

      【解决方案3】:

      如果我正在阅读您的描述,我发现您正在尝试使用变通方法(try-catch)来解决某些问题,而不是实际解决问题。

      您知道用户可能会填写错误的路径,因此您需要对其进行检查,如果正确则开始搜索,或者停止代码并向用户反馈路径不正确。

      我不确定您是如何构建代码的,但我会这样做。

      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);
                       
                  
      

      【讨论】:

      • 你仍然需要尝试/捕捉。可以在 Exists 检查之后和下一行之前删除该目录。总会有竞争条件,但不应依赖 soley 对流控制的例外情况
      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2011-11-02
      • 2021-12-17
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多