简体   繁体   中英

How can i fix this kind of exception? ArgumentOutOfRangeException

The exception message returns:

Year, Month, and Day parameters describe an un-representable DateTime.

The exception hits the monthCalendar1.SelectionStart

            DateTime check_date = monthCalendar1.SelectionStart;

            for (int index = 0; index <= 5; index++)
            {
                check_date = new DateTime(check_date.Year, check_date.Month, check_date.Day - index);

            }

I also tried this one. but another exception has occured TargetInvocationException

                DateTime check_date = monthCalendar1.SelectionStart;

                for (int index = 0; index <= 5; index++)
                {
                    check_date = check_date.AddDays(-index);

                }

Exception details:

System.ArgumentOutOfRangeException was unhandled Message=Year, Month, and Day parameters describe an un-representable DateTime. Source=mscorlib StackTrace: at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day) at System.DateTime..ctor(Int32 year, Int32 month, Int32 day) at TestGetDates.Program.Main(String[] args) in C:\\Documents and Settings\\ngd11\\My Documents\\Visual Studio 2010\\Projects\\TestGetDates\\TestGetDates\\Program.cs:line 15 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

  date_tax.Day - index

这可能会产生一个负数,导致此异常,调试并检查其值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM