python学习笔记(十五)-异常处理

money = input('输入多少钱:')
months = input('还几个月:')
try:
    res = calc(int(money),int(months))
except ZeroDivisionError as e:  #try里面的代码如果出错了,走except里面的代码
    traceback.print_exc()#只是输出报错的详细信息而已
    print('还款的月数不能小于1',e) #months输入0时
except ValueError as e:
    print('输入必须是整数,%s'%e) #moneymonths输入非整数时
except Exception as e:  #捕获所有的异常
    print('未知错误!%s'%e)
else:#没有出错的情况下走else
    print('每个月应该还%s'%res) 
print('=====不管是否出错都要走这边!!!')
原文地址:https://www.cnblogs.com/lincy/p/8490514.html