c# - 如何设置 try-catch 以确保用户输入的值包含在枚举列表中?

标签 c# enums

我有一个星期几的枚举列表,以及一个要求用户输入当前日期的程序(它可以是列表中的任何一天)。我需要实现 try-catch 以确保用户输入有效的一天,但我遇到了一些困惑。

当我搜索如何使用枚举处理异常时,大多数站点建议使用 TryParse 而不是 try-catch,但对于这个特定的任务,我需要将其包装在 try-catch 中。我应该使用 catch (OverflowException) 吗?这似乎不起作用,因为我不确定之后会发生什么。我试图在 OverflowException 之后立即声明一个变量,就像我在 try-catch 示例中看到的那样;但是,我将其删除,因为它给了我错误。到目前为止,我还没有找到与我类似的使用try-catch的例子,可以借鉴一下。

有人可以看看这个程序并帮助我如何正确地包含一个 try-catch 来处理无效输入吗?如果用户输入星期日到星期六以外的任何内容(包括缩写),我希望控制台说“不是有效的一天”。谢谢!!

 class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please enter the current day of the week.");
                string currentday = Console.ReadLine();
                Console.WriteLine("Good job.  Today is " + currentday);
                Console.ReadLine();

            }
           catch(OverflowException)
            {
                Console.WriteLine("Not a valid day");
            }
            Console.ReadLine();
        }

        public enum days
        {
            Sunday,
            Monday,
            Tuesday,
            Wednesday,
            Thursday,
            Friday,
            Saturday          
        }

最佳答案

您可以使用 EnumTryParse Method .您实际上也可以删除 try-catch。

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Please enter the current day of the week.");
            string currentday = Console.ReadLine();
            days day;

            if (Enum.TryParse<days>(currentday, out day))
            {
                Console.WriteLine("Good job.  Today is " + currentday);
                Console.ReadLine();
            }
            else
            {
                throw new OverflowException(); // try parse failed. enum not valid! Why OverflowException?
            }
        }
        catch (OverflowException)
        {
            Console.WriteLine("Not a valid day");
        }
        Console.ReadLine();
    }

    public enum days
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
}

static void Main(string[] args)
{
    Console.WriteLine("Please enter the current day of the week.");
    string currentday = Console.ReadLine();
    days day;

    if (Enum.TryParse<days>(currentday, out day))
    {
        Console.WriteLine("Good job.  Today is " + currentday);
    }
    else
    {
       Console.WriteLine("Not a valid day");
    }

    Console.ReadLine();
}

编辑:

进一步检查,@Nalaka526 的答案更合适。 Enum.TryParse 将为“01”、“111”等意外输入返回 true,而 Enum.IsDefined 将仅匹配枚举名称。

下面是一种方式,您可以在输入有效之前继续提示这一天。

static void Main(string[] args)
{
    bool isValid = false;

    do {
        Console.WriteLine("Please enter the current day of the week.");
        string currentday = Console.ReadLine();

        isValid = Enum.IsDefined(typeof(days), currentday);

        if (isValid)
        {
            Console.WriteLine("Good job.  Today is " + currentday);
        }
        else
        {
            Console.WriteLine("Not a valid day");
        }
    } while (!isValid);            

    Console.ReadLine();
}

关于c# - 如何设置 try-catch 以确保用户输入的值包含在枚举列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52732117/

相关文章:

ios - Xcode 显示不需要的语法错误

c# - AutoMapper 5.2如何配置

c# - 清除私有(private)收藏或将其设置为空?

c# - 如何设置默认 NavigationUI 的样式

c# - Sitecore 上传,使用项目语言而不是上下文语言

c# - MVC3 HiddenFor 与枚举。输入的值是枚举键而不是值?

java - 无法使用 ClassLoader 加载枚举类

python - 如何访问 Django 模板中的枚举类型

ios - 使用自定义枚举作为值在 Swift 中创建字典 'on-the-fly'

c# - 在 ListView 中加载并显示所有项目后会触发哪个事件?