Python基础合集 练习22 (错误与异常处理语句2)
‘’’
try:
语句块
except:
语句块2
else
‘’’
class Mobe1():
def init(self) -> None:
pass
def mob1(self):
while True:
try:
num = int(input('请输入一个数: '))
result = 50 / num
print(result)
print('50/{0}={1}'.format(num, result))
except (ZeroDivisionError, ValueError) as e: # 将可能出错的异常放在一起
print('程序错误,原因是: ', e)
else:
print('没有出现任何异常,程序顺利执行完毕!')
exit() # 退出
‘’’
try:
语句块
except:
语句块2
finally:
语句块3
‘’’
class Mobe2():
def init(self) -> None:
pass
def mob(self):
num1 = int(input('请输入一个数: '))
i = 90
try:
resu = i / num1
print(resu)
print('{0}/{1}={2}'.format(i, num1, resu))
except (ZeroDivisionError, ValueError) as e:
print('程序出错,原因是: ', e)
finally: # 一定会执行的语句
print('无论如何都会执行的语句')
mobe1 = Mobe1()
mobe2 = Mobe2()
mobe2.mob()
mobe1.mob1()
总代码如下:
谢谢观看,制作不易,不喜勿喷
如果喜欢,请点赞加关注哟
小白们,可以照着敲一遍哈