Question

private bool ValidDateCheck(){

 bool _isValid = true;
 try{
  DateTime.Parse("07/&*/2009");
  DateTime d = Convert.ToDateTime("07/&*/2009");
 }
 catch{
  _isValid = false;
 }
 return _isValid;
}

How does the above code not throw an exception?

Was it helpful?

Solution

This is from the .NET 1.1 documentation:

The string s is parsed using the formatting information in a DateTimeFormatInfo initialized for the current culture.

This method attempts to parse s completely and avoid throwing FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time. If s contains only a date and no time, this method assumes 12 A.M. Any leading, inner, or trailing white space character in s is ignored.

Parameter s must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.

I am unable to test this as the earliest version I can build with in VS2008 is 2.0.

OTHER TIPS

The code doesn't throw an exception because exceptions are thrown at run time, and this code never runs. I know it never runs because there's no way it would compile with single quotes for the string literals. You need to use double quotes for the strings.

If the single quotes are just a typo, please edit the question and I'll take another look.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top