Exceptional handling for Key Error

#Without Exceptional handling program terminates if key b not found
s={'d':2,'k':4}
if b in s.keys():
    s['b'].append[9]
else:
    s['b']= 8


#Exceptional handling for key error and program run without termination
s={'d':2,'k':4}
try:
    s['b'].append[9]
except KeyError:
    s['b']= 8

No comments: