Get key name from Python KeyError exception

You can use e.args:

[53]: try:
    x2 = myDict['key2']
except KeyError as e:    
    print e.args[0]
   ....:     
key2

From the docs:

The except clause may specify a variable after the exception name (or
tuple). The variable is bound to an exception instance with the
arguments stored in instance.args

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)