Year out of range

I have an app having a few thousand users and two of them send an email about an error, one “Year out of range”, the other mailed “Month out of range”. I am not able to reproduce the error. Any clues?

Where are they located? Are the using a non-Gregorian calendar?

Are you building a datetime object from a string. If so you could be slicing the string in a bad way. In the US dates are typically:

mm/dd/yyyy

In the UK, and other places:

dd/mm/yyyy

That, if sliced badly would result in a month over 12?

China has dates as:

yyyymmdd

Yes. If the OP has an international set of users he really needs to allow the user to select a date in a non-ambiguous way, perhaps using popups populated with days of the month, month names, and years.

Look into DateTime.FromString:

MyVariable = DateTime.FromString( UserInputValue, Locale.Current, TimeZone.Current )

which will convert the date using the users defined format.

If you are using data from a file then store it in a known format (if you are able to control the file). For example SQLDateTime.

Thanks everyone, it appears the issue occurs when parsing non-standard EXIF data…

Wrap it in a Try… Catch block and respond accordingly when it goes wrong.

1 Like

Thanks, never used try…catch before, useful for these kinds of issues. Case closed.

Try Catch is definitely ideal for small operations that may just go wrong, just like this. You can deal with the expected issues locally and continue after fixing the problem.

It can also be handy in a larger context. For example, if you have a file load method exceptions can be dealt with on a global Application.UnhandledException handler, however, if you are designing a preview window that uses that same method, you can wrap the call in a Try Catch block and handle the exception is a different way, more friendly to a preview dialog. If that makes any sense.