try…except…else 结构
try…except…else 结构增加了“else 块”。如果 try 块中没有抛出异常,则执行 else 块。如果try 块中抛出异常,则执行 except 块,不执行 else 块。
【例】try…except…else 结构的使用
while True:
try:
a = input('请输入被除数:')
if a == 'quit':
print('退出程序。')
break
b = input('请输入除数:')
c = float(a) / float(b)
except BaseException as e:
print(e)
else:
print('除法运算的结果是:{}'.format(c))
运行结果: