【问题标题】:C# Try/Catch from User Input Checking for Enum List [duplicate]C# Try/Catch 从用户输入检查枚举列表 [重复]
【发布时间】:2021-04-09 14:04:41
【问题描述】:

我正在努力学习 C#,但我真的很挣扎。我非常需要帮助。我有这个任务将语句包装在 try/catch 块中并让它打印“请输入一周中的实际日期”。如果发生错误,发送到控制台。我设法根据枚举确定用户输入是否为真,但我无法捕捉到错误的部分。任何帮助将不胜感激。

我在名为 OneWeek 的单独类中创建了枚举列表,这是我的代码:

        Console.WriteLine("Please enter the current name of the day.");
        string input = Console.ReadLine();
        string test = input.ToLower();
        OneWeek.DaysOfTheWeek dayParse;
        bool inputParse = Enum.TryParse(test, out dayParse);
        try
        {

            if (inputParse == true)
            {
                Console.WriteLine("Thank God it's " + input);
            }

        }

        catch (FormatException)
        {
            Console.WriteLine("Please enter an actual day of the week.");
            
        }

        catch (Exception)
        {
            Console.WriteLine("Please enter an actual day of the week.");
            
        }
        Console.ReadLine();
        return;

感谢所有回答的人!尝试了所有建议,它有所帮助。我还在建议的线程上找到了答案。你们让我很开心。谢谢!

这是我的代码的最终结果。只是改变了一些变量,但目标是一样的。

try
        {
            Console.WriteLine("Please enter the current name of the day.");
            string input = Console.ReadLine();
            string test = input.ToUpper();

            OneWeek.DaysOfTheWeek day;

            if (Enum.TryParse<OneWeek.DaysOfTheWeek>(test, out day))
            {
                Console.WriteLine("You are right! Today is  " + test + "!");
            }

            else
            {
                throw new OverflowException();
            }
     

        }

        catch (OverflowException)
        {
            Console.WriteLine("Please enter an actual day of the week.");
            
            
        }

        Console.ReadLine();
        return;

【问题讨论】:

  • 如果你使用Enum.TryParse,你不需要try/catch,这个方法不会抛出,这就是它被创建的原因。
  • if (Enum.TryParse(Console.ReadLine(), out OneWeek.DaysOfTheWeek dayParse)){}else{}... TBH 你有很多不需要的变量,并且为了简单的检查而膨胀。
  • 您不应该以这种方式使用异常。异常是针对实际发生的错误,而不是用于控制程序的流程。你有点暗示这一点:“我无法抓住虚假的部分”。这是因为您应该测试 false,而不是抛出异常。
  • 即使我给出了答案,我也会补充,@tnw 是 100% 正确的。除了学习,别把这当成一个好策略。

标签: c# enums try-catch


【解决方案1】:
    Console.WriteLine("Please enter the current name of the day.");
    string input = Console.ReadLine();
    string test = input.ToLower();
    if(Enum.TryParse(test, out var dayParse))
        Console.WriteLine("Thank God it's " + input);
    else
        Console.WriteLine("Please enter an actual day of the week."); 
  
    Console.ReadLine();
    return;

【讨论】:

    【解决方案2】:

    您实际上不会遇到异常,因为您正在相当安全地处理您的值(好)。

    如果你真的想要一个异常,你需要抛出一个:

    if (inputParse == true)
    {
        Console.WriteLine("Thank God it's " + input);
    }
    else 
    {
       throw new FormatException("Please Enter a real day of the week");
    }
    

    然后我还将捕获更改为:

    catch (FormatException fex)
    {
        Console.WriteLine(fex.Message);            
    }
    

    或者,也可以不那么安全地处理枚举:

    try
    {
       OneWeek.DaysOfTheWeek dayParse = Enum.Parse(test);
    }
    catch (Exception ex)
    {
       // write error
    }
    

    【讨论】:

    • You aren't actually going to get an exception OP 不会看到它,因为 TryParse 处理它;仍然抛出异常,但已处理。
    • if (inputParse == true) 不是一个好的风格。写if (inputParse)。
    • @OlivierJacot-Descombes 我尝试尽可能多地复制和粘贴原始代码以显示它所替换的内容。
    • @AustinTFrench 我也同意,当人们看到它时留下建设性的反馈并没有错:)。预计答案不一定包括“这就是我将如何做”(对此有很多意见),而是解决手头的问题。关于“为什么”它不是一种好的风格的解释将不胜感激,以便人们理解。
    • @Codexer 你在哪里看到的? ` If ( Enum.TryParse(test, out foo)) { ... } else { throw new exception() }` 会捕获...
    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多