c#超出范围错误

时间:2015-11-18 23:20:15

标签: c# .net visual-studio datetime

我创建了自己的DateTime类,它似乎正在运行。我只是想知道如何在我的控制台中为我的ArgumentOutOfRangeExceptions打印错误?我不确定如何做到这一点,所以我想要一些帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace date
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\t\t\t\t\t\tNextDate Application\n\t\t\t\t\t-------------------------------------");

            Console.WriteLine("please enter date as dd/MM/yyyy");
            int day;
            int month;
            int year;

            string[] read = Console.ReadLine().Split('/');
            day = int.Parse(read[0]);
            month = int.Parse(read[1]);
            year = int.Parse(read[2]);

            Date date = new Date(day, month, year);
            Console.WriteLine("{0}/{1}/{2}", date.Day, date.Month, date.Year);
            Console.ReadLine();
        }

        class Date
        {
            private int _month; // 1-12
            private int _day; // 1-31 depending on month
            private int _year;

            public Date(int day, int month, int year)
            {
                Month = month;
                Day = day;         
                Year = year;
            }

            public int Year
            {
                get { return _year; }
                set
                {
                    if (value >= 1820 && value <= 2020)
                        _year = value;
                    else
                        throw new ArgumentOutOfRangeException("year", value, "year out of range");
                }
            }

            public int Month
            {
                get { return _month; }
                set
                {
                    if (value > 0 && value <= 12)
                        _month = value;
                    else
                        throw new ArgumentOutOfRangeException("Month", value, "Month must be 1-12");
                }
            }

            public int Day
            {
                get { return _day; }
                set
                {

                    int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

                    if (value > 0 && value <= days[_month])
                        _day = value;

                    else if (_month == 2 && value == 29 &&
                        _year % 400 == 0 || (_year % 4 == 0 && _year % 100 != 0))
                        _day = value;
                    else
                        throw new ArgumentOutOfRangeException("Day", value, "Day is out of range");


                }

            }
        }
    }
}

2 个答案:

答案 0 :(得分:4)

只是抓住你抛出的例外

try
{
    Date date = new Date(day, month, year);
}
catch(ArgumentOutOfRangeException exc)
{
    Console.WriteLine(exc.Message);
}

另请注意,您必须处理用户输入:您假设他将插入一个字符串"NN/NN/NNNN"。如果他插入"NN/NN"怎么办?还是"this/will/crash"

答案 1 :(得分:1)

鉴于您目前的设计,您可以这样做:

try
{
    Date date = new Date(day, month, year);
    Console.WriteLine("{0}/{1}/{2}", date.Day, date.Month, date.Year);
}
catch (ArgumentOutOfRangeException ex)
{
    Console.WriteLine(ex.Message);
}

请注意,重新发明轮子是没有必要的(除非您有意进行实验)...使用正确的内置.NET结构(如TryParseExact而不是抛出和捕获异常)将为您节省很麻烦:

Console.WriteLine("please enter date as dd/MM/yyyy");

DateTime date;
if (DateTime.TryParseExact(Console.ReadLine(), "dd/MM/yyyy", null, DateTimeStyles.None, out date))
{
    Console.WriteLine("Correct date: {0}", date.ToString("dd/MM/yyyy"));
}
else
{
    Console.WriteLine("Invalid date!");
}

Console.ReadLine();