【问题标题】:Error handling is interfering with the Recursive method in C# [duplicate]错误处理干扰了 C# 中的递归方法 [重复]
【发布时间】:2021-08-11 02:32:19
【问题描述】:

我正在创建一个目录中的文件列表。在创建该列表之前,我首先要进行错误检查以确保在控制台应用程序中输入的目录有效。我正在使用递归来创建一个循环,这样如果目录无效,它将通过验证方法运行。

从我的 Program.cs 类开始,我使用 GetFileListFromDirectory 方法。然后该方法转到 IsValidDirectory 方法。如果我第一次输入一个好的目录路径一切正常。当我使用错误目录进行测试时,尽管我的输入正确地通过 IsValidDirectory 方法运行并给了我一个错误;所以我在控制台中输入了一个新目录,该方法循环回到 IsValidDirectory 的开头(就像它应该的那样)并验证新目录是否正确,但是当它返回时,它会跳回带有原始目录的 Catch 行 IsValidDirectory坏目录。完成此操作后,它会返回 GetFileListFromDirectory 并停留在 while 循环中,因为此时 isValid 仍然为 false,而此时它实际上应该为 true。

感谢您的帮助!

/* Creates a list of all of the files in the chosen directory for mass audit potential */
        public List<string> GetFileListFromDirectory(string inputDirectory)
        {
            bool isValid = false; //makes sure the directory is validated before continuing the method

            while (!isValid)
            {
                isValid = IsValidDirectory(inputDirectory);
            }

            DirectoryInfo directory = new DirectoryInfo(inputDirectory);
            FileInfo[] folder = directory.GetFiles("*");

            List<string> files = new List<string>();

            if (isValid)
            {
                foreach (var file in folder)
                {
                    files.Add(file.Name.ToString().Replace(".csv", "").Replace(".txt", ""));
                }

                return files;
            }

            return null; //code path does not allow this return
        }

        /* Method keeps looping back to GetFileListFromDirectory if the directory results in error */
        public bool IsValidDirectory(string inputDirectory)
        {
            bool validDirectory;

            try
            {
                DirectoryInfo directory = new DirectoryInfo($@"{inputDirectory}");
                FileInfo[] folder = directory.GetFiles("*");

                validDirectory = true;
            }
            catch(Exception e)
            {
                Console.WriteLine($"\n{e}\n\nCheck the directory path and enter it again: ");

                IsValidDirectory(Console.ReadLine());

                validDirectory = false;
            }

            return validDirectory;
        }

【问题讨论】:

  • “我正在使用递归来创建循环”——嗯,这就是你的问题。不要那样做。

标签: c# recursion error-handling return boolean


【解决方案1】:

而不是递归调用IsValidDirectory,您需要将调用移动到调用方法。尝试将 while 块更改为:

        while (true)
        {
            if (IsValidDirectory(inputDirectory))
            {
                break;
            }

            Console.WriteLine($"\n\nCheck the directory path and enter it again: ");
            inputDirectory = Console.ReadLine();
        }

并删除 catch 块的前两行,只留下:

        catch (Exception e)
        {
            validDirectory = false;
        }

【讨论】:

    【解决方案2】:

    我可以看到你被绊倒的地方。问题是输入目录不是通过引用传递的。退出该循环的唯一方法是在 GetFileListFromDirectory 方法中更改 inputdirectory 值。实现这一点的简单方法是将控制台读取线移动到您的内部,同时循环设置 inputDirectory 值。

    例如(只要确保从 isvalid 方法中删除 readline:

    while(!isValid)
    {
        isValid = IsValidDirectory(inputDirectory);
        if(!isValid)
        {
            inputDirectory = Console.ReadLine();
        }
    }
    

    【讨论】:

    • 建议使用 Niel T 的答案。干净多了。
    猜你喜欢
    • 2012-04-20
    • 2015-10-04
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多