Python--异常处理

# import traceback
# import pymysql


# def calc(a, b):
# res = (a / b)
# return res
#
#
# def main():
# money = input('输入多少钱:')
# months = input('还几个月:')
# try:
# res = calc(int(money), int(months))
# except ZeroDivisionError as e: # try里面的代码出错了,走excel里面的代码
# traceback.print_exc() # 只是输出报错的详细信息
# print('还款的月数不能小于1', e)
# except ValueError as e:
# print('输入必须是整数', e)
# except Exception as e: # 捕获所有的异常
# print('未知错误!', e)
# else: # try里面的代码没有出错,走else
# print('每个月应该还%s' % res)
# print('hahahaha')
#
#
# def main2():
# try:
# conn = pymysql.connect(host='122.932.122.11', user='root', password='123456', db='test')
# except Exception as e:
# print('数据库连接不了', e)
# else:
# cur = conn.cursor()
# sql = 'select * from a;'
# try:
# cur.execute(sql)
# except Exception as e:
# print('sql语句有错误!%s,sql是%s' % (e, sql))
# else:
# res = cur.fetchall()
# return res
# finally: # 不管有没有捕捉到异常,都会走这里
# cur.close()
# conn.close()


# try:
# a = int(input('输入:'))
# b = int(input('输入:'))
# res = a / b
# except Exception as e:
# print(e)
# else:
# print(res)
# finally:
# print('什么时候到我这里呢')

import requests


def req():
r = requests.get('http://api.nnzhp.cn/api/user/all_stu', headers={'Referer': 'http://api.nnzhp.cn/'})
if len(r.json()['stu_info']) < 0:
pass
else:
raise Exception('主动抛出异常!') # 主动抛出异常
print('hahahaha')


req()

转载于:https://www.cnblogs.com/wangsilei/p/8436033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值