If you’ve ever noticed how completely unreliable computers are – you know, performing completely random and unpredictable actions when given a simple, explicit instruction – then you’ll appreciate today’s snippet from John P.
Today’s code was uncovered in a web application and attempts to solve the well-known problem of DateTime.Parse: in the event that the method fails the first time, it will probably correctly parse a string the tenth time.
DateTime DateTimeParseSafe(string s)
{
return DateTimeParseSafe(s, 0);
}
DateTime DateTimeParseSafe(string s, int iter)
{
DateTime returnVal = DateTime.MinValue;
try
{
returnVal = DateTime.Parse(s);
}
catch
{
if (iter < 10)
{
iter++;
returnVal = DateTimeParseSafe(s, iter);
}
}
return returnVal;
}