10.1 除零异常
i = input("请输入数字:")
n = 8888
result = n / int(i)
print(result)
print("{0}除以{1}等于{2}".format(n,i,result))
10.2 捕获异常
i = input("请输入数字:")
n = 8888
try:
result = n / int(i)
print(result)
print("{0}除以{1}等于{2}".format(n,i,result))
except ZeroDivisionError as e:
print("不能除以0,异常:{}".format(e))
10.2.2 多个except代码块
i = input("请输入数字:")
n = 8888
try:
result = n / int(i) #表达式可能发生除0异常。另外,int(i)表达式也可能发生整数转换异常
print(result)
print("{0}除以{1}等于{2}".format(n,i,result))
except ZeroDivisionError as e: #捕获零异常
print("不能除以0,异常:{}".format(e))
except ValueError as e: #捕获整数异常
print("输入的是无效数字,异常:{}".format(e))
10.2.3 多重异常捕获
i = input("请输入数字