C#从用户输入检查枚举列表的Try/Catch

C#从用户输入检查枚举列表的Try/Catch,c#,enums,try-catch,C#,Enums,Try Catch,我正在努力学习C#,我真的很努力。我急需帮助。我的任务是将语句包装在try/catch块中,并在出现错误时将其打印到控制台的“请输入一周中的实际日期”。我根据枚举成功地获取了用户输入是否为真,但无法捕获错误部分。任何帮助都将不胜感激 我在名为OneWeek的单独类中创建了枚举列表,这是我的代码: Console.WriteLine("Please enter the current name of the day."); string inpu

我正在努力学习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;
试试看
{
Console.WriteLine(“请输入当天的当前名称”);
字符串输入=Console.ReadLine();
字符串测试=input.ToUpper();
一周的一天为一周的一天;
if(枚举锥虫(测试,外出日))
{
WriteLine(“你说得对!今天是“+test+”!”);
}
其他的
{
抛出新的OverflowException();
}
}
捕获(溢出例外)
{
Console.WriteLine(“请输入一周中的实际日期”);
}
Console.ReadLine();
返回;

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

如果确实需要异常,则需要抛出一个异常:

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
}

如果使用的是
Enum.TryParse
,则不需要try/catch,此方法不会抛出,这就是创建此方法的原因。
if(Enum.TryParse(Console.ReadLine(),out one week.DaysOfTheWeek dayParse)){}else{}
。。。TBH您有许多不需要的变量,并且为了一个简单的检查,这些变量会膨胀很多。您不应该以这种方式使用异常。例外情况是发生的实际错误,而不是控制程序流。你有点暗指:“我抓不到虚假的部分”。这是因为您应该测试false,而不是抛出异常。即使我给出了答案,我也要补充一点,@tnw是100%正确的。除了学习,不要把这当作一个好的策略。
你实际上不会遇到异常
OP不会看到它,因为
TryParse
会处理它;仍然会引发异常,但已处理。
如果(inputParse==true)
不是一种好的样式。写
if(inputParse)
@OlivierJacot Descombes我尽可能多地复制和粘贴原始代码,以显示它所替代的内容。@AustinFrench我也同意,看到有建设性的反馈意见时留下它没有错:)。答案不一定包括“我会怎么做”(对此有很多意见),而是解决手头的问题。如果能解释一下“为什么”这不是一种好的风格,我们将不胜感激。@Codexer你在哪里看到的如果(Enum.TryParse(test,out foo)){…}否则{throw new exception()}`将捕获。。。
    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;