Using Redhat Python 2.7.5 I'm trying to parse a date in YYYY-MM-DD
format using datetime.datetime.strptime
and I intermitently get a tuple out of range error as follows:
client.py in parse_date(d='2014-12-05')
138 return dt.datetime.strptime(d,"%Y-%m-%d")
139 except:
140 raise Exception("Unexpected Date: '{0}' ({1})".format(d), e)
undefined, d = '2014-12-05', e undefined
<type 'exceptions.IndexError'>: tuple index out of range
args = ('tuple index out of range',)
message = 'tuple index out of range'
That the format string looks correct, and the intermittent nature of the problem suggests some sort of threading issue, but to be honest I've no idea, nor any idea how to reliably reproduce the error. Any suggestions to resolve this?
The problem I think is with the way you catch the exception:
except:
raise Exception("Unexpected Date: '{0}' ({1})".format(d), e)
Here, you don't pass the variable e
to format
correctly. Instead, it should be
except:
raise Exception("Unexpected Date: '{0}' ({1})".format(d, e))
Also, in case e
happens to be the exception raise, you will need to explicitly get a variable as below:
except Exception as e:
raise Exception("Unexpected Date: '{0}' ({1})".format(d), e)
Finally, it is better to capture the specific error which can be raised by the try block, so you should do
except ValueError as e:
raise Exception("Unexpected Date: '{0}' ({1})".format(d), e)