练习以下异常处理结构
目录
try...except...
参考代码:
while True:
x = input('请输入一个整数:')
try:
x = int(x)
print('你输入了整数{0}'.format(x))
break
except Exception as e:
print('输入错误,请重新输入!!!')
运行结果:
try...except...else...
参考代码:
while True:
x = input('请输入一个整数:')
try:
x = int(x)
except Exception as e:
print('输入错误,请重新输入!!!')
else:
print('你输入了整数{0}'.format(x))
break
运行结果:
try...except...finally...
参考代码:
try:
x = input('请输入一个整数:')
x = int(x)
except Exception as e:
print('输入错误!!!')
finally:
print('你输入了{0}'.format(x))