Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching KeyError message in Python 3

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
like image 208
Arda Arslan Avatar asked Sep 05 '25 03:09

Arda Arslan


1 Answers

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__().
    */
like image 112
Thierry Lathuille Avatar answered Sep 07 '25 19:09

Thierry Lathuille