码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记(二十):异常处理

时间:2018-02-24 18:01:22      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:异常   必须   one   error   rac   多少   bsp   return   roo   

 1 def calc(a,b):
 2     res=a/b
 3     return res
 4 def main():
 5     money=input(‘输入多少钱:‘)
 6     months=input(‘还几个月:‘)
 7     try:
 8         res=calc(int(money),int(months))
 9     except ZeroDivisionError as e:#try里面的代码如果出错了,走except里的代码
10         print(‘还款月数不能小于1‘,e)
11     except ValueError as e:
12         print(‘输入必须是整数,%s‘%e)
13     else:#没有出错的情况下走else
14         print(‘每个月应该还%s‘%res)
15 main()

 

 1 def calc(a,b):
 2     res=a/b
 3     return res
 4 def main():
 5     money=input(‘输入多少钱:‘)
 6     months=input(‘还几个月:‘)
 7     try:
 8         res=calc(int(money),int(months))
 9     except Exception as e:
10         print(‘输入错误,请检查输入!%s‘ % e)#出错就报异常,不分具体的情况
11 main()
 1 import traceback
 2 def calc(a,b):
 3     res=a/b
 4     return res
 5 def main():
 6     money=input(‘输入多少钱‘)
 7     months=input(‘还几个月:‘)
 8     try:
 9         res=calc(int(money),int(months))
10     except ZeroDivisionError as e:#try里面的代码如果出错了,走except里的代码
11         traceback.print_exc()#只是输出报错的详细信息而已
12         print(‘还款月数不能小于1‘,e)
13     except ValueError as e:
14         print(‘输入必须是整数,%s‘%e)
15     else: #没有出错的情况下走else
16         print(‘每个月应还%s‘%res)
17     print(‘hahahaa‘)
18 main()
 1 import pymysql
 2 def main2(sql):
 3     try:
 4         conn=pymysql.connect(host=‘122.932.122.11‘,user=‘root‘,password=‘123456‘,db=‘test‘)
 5     except Exception as e:
 6         print(‘数据库连接不了,%S‘%e)
 7     else:
 8         cur=conn.cursor()
 9         try:
10             cur.execute(sql)
11         except Exception as e:
12             print(‘sql语句有错误!%s是"%s‘%(e,sql)‘)
13         else:
14             res=cur.fetchall()
15             return res
16         finally:#不管有没有捕捉到异常,都会走这里
17             cur.close()
18             conn.close()
 1 try:
 2     a=int(input(‘xx:‘))
 3     b=int(input(‘sss:‘))
 4     res=a/b
 5 except Exception as e:
 6     print(e)
 7 else:
 8     print(res)
 9 finally:
10     print(‘什么时候到我这里呢‘)
 1 import requests
 2 def req():
 3     r=requests.get(‘http://api.nnzhp.cn/api/user/all_stu‘,headers={‘Referer‘:‘http://api.nnzhp.cn/‘})
 4     print(r.json())
 5     print(r.json()[‘stu_info‘])
 6     if len(r.json()[‘stu_info‘])>0:
 7         pass
 8     else:
 9         raise Exception(‘这个接口什么数据都没有‘)#主动抛出异常
10 req()
 1 import requests
 2 def req():
 3     r = requests.get(‘http://api.nnzhp.cn/api/user/all_stu‘, headers={‘Referer‘: ‘http://api.nnzhp.cn/‘})
 4     # print(r.json())
 5     # print(r.json()[‘stu_info‘])
 6     if len(r.json()[‘stu_info‘]) <0:
 7         pass
 8     else:
 9         raise Exception(‘这个接口什么数据都没有‘)  # 主动抛出异常
10         # raise ValueError
11 req()

 

python学习笔记(二十):异常处理

标签:异常   必须   one   error   rac   多少   bsp   return   roo   

原文地址:https://www.cnblogs.com/wxcx/p/8466828.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!