Fix error in attribute setting
Narrow down types of exceptions which are handled

Handle only KeyError exception, because this is the only type of
exception expected.