I'm having trouble catching the message in a KeyError
in Python 3 (More specifically 3.5.3).
Input:
try:
raise KeyError('some error message')
except KeyError as e:
if str(e) == 'some error message':
print('Caught error message')
else:
print("Didn't catch error message")
Output:
Didn't catch error message
Oddly, the same thing works with something like an IndexError
Input:
try:
raise IndexError('some index error message')
except IndexError as e:
if str(e) == 'some index error message':
print('Caught IndexError message')
else:
print("Didn't catch IndexError message")
Output:
Caught IndexError message
Also, this problem seems to be specific to Python 3 because you could just grab the message
attribute in Python 2.
Input:
try:
raise KeyError('some error message')
except KeyError as e:
if e.message == 'some error message':
print 'Caught error message'
else:
print "Didn't catch error message"
Output:
Caught error message
This behavior is perfectly explained in the source code:
static PyObject *
KeyError_str(PyBaseExceptionObject *self)
{
/* If args is a tuple of exactly one item, apply repr to args[0].
This is done so that e.g. the exception raised by {}[''] prints
KeyError: ''
rather than the confusing
KeyError
alone. The downside is that if KeyError is raised with an explanatory
string, that string will be displayed in quotes. Too bad.
If args is anything else, use the default BaseException__str__().
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With