dict1 = {‘a’: 1, ‘b’: 2, ‘v’: 22}
try:
x = dict1[‘y’]
except LookupError:
print(‘查询错误’)
except KeyError:
print(‘键错误’)
else:
print(x)
查询错误
dict1 = {'a': 1, 'b': 2, 'v': 22}
try:
x = dict1['y']
except KeyError:
print('键错误')
except LookupError:
print('查询错误')
else:
print(x)
``
try:
raise NameError('HiThere')
except NameError:
print('An exception flew by!')