python学习笔记(二十):异常处理
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里面的代码如果出错了,走except里的代码
print('还款月数不能小于1',e)
except ValueError as e:
print('输入必须是整数,%s'%e)
else:#没有出错的情况下走else
print('每个月应该还%s'%res)
main()
def calc(a,b):
res=a/b
return res
def main():
money=input('输入多少钱:')
months=input('还几个月:')
try:
res=calc(int(money),int(months))
except Exception as e:
print('输入错误,请检查输入!%s' % e)#出错就报异常,不分具体的情况
main()
import traceback
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里面的代码如果出错了,走except里的代码
traceback.print_exc()#只是输出报错的详细信息而已
print('还款月数不能小于1',e)
except ValueError as e:
print('输入必须是整数,%s'%e)
else: #没有出错的情况下走else
print('每个月应还%s'%res)
print('hahahaa')
main()
import pymysql
def main2(sql):
try:
conn=pymysql.connect(host='122.932.122.11',user='root',password='',db='test')
except Exception as e:
print('数据库连接不了,%S'%e)
else:
cur=conn.cursor()
try:
cur.execute(sql)
except Exception as e:
print('sql语句有错误!%s是"%s'%(e,sql)')
else:
res=cur.fetchall()
return res
finally:#不管有没有捕捉到异常,都会走这里
cur.close()
conn.close()
try:
a=int(input('xx:'))
b=int(input('sss:'))
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/'})
print(r.json())
print(r.json()['stu_info'])
if len(r.json()['stu_info'])>0:
pass
else:
raise Exception('这个接口什么数据都没有')#主动抛出异常
req()
import requests
def req():
r = requests.get('http://api.nnzhp.cn/api/user/all_stu', headers={'Referer': 'http://api.nnzhp.cn/'})
# print(r.json())
# print(r.json()['stu_info'])
if len(r.json()['stu_info']) <0:
pass
else:
raise Exception('这个接口什么数据都没有') # 主动抛出异常
# raise ValueError
req()
python学习笔记(二十):异常处理的更多相关文章
- python 学习笔记二十 django项目bbs论坛
项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可 ...
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
- python3.4学习笔记(二十五) Python 调用mysql redis实例代码
python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...
- python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法
python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...
- python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字
python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字在字符串里面插入指定分割符的方法,先把字符串变成list然后用join方法变成字符串str=' ...
- python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法
python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法 在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数 ...
- Python学习笔记(十四)
Python学习笔记(十四): Json and Pickle模块 shelve模块 1. Json and Pickle模块 之前我们学习过用eval内置方法可以将一个字符串转成python对象,不 ...
- Python学习笔记(十)
Python学习笔记(十): 装饰器的应用 列表生成式 生成器 迭代器 模块:time,random 1. 装饰器的应用-登陆练习 login_status = False # 定义登陆状态 def ...
- python学习笔记(二)、字符串操作
该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.字符串基本操作 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于 ...
- Python 学习笔记二
笔记二 :print 以及基本文件操作 笔记一已取消置顶链接地址 http://www.cnblogs.com/dzzy/p/5140899.html 暑假只是快速过了一遍python ,现在起开始仔 ...
随机推荐
- 在sql中使用函数,遇到net.sf.jsqlparser.parser.ParseException异常
异常详情如下 Caused by: net.sf.jsqlparser.parser.ParseException: Encountered " "->" &quo ...
- element-ui走马灯如何实现图片自适应 长度和高度 自适应屏幕大小
最近写用vue2.0写一个项目,用到了走马灯效果,由于项目赶时间,想偷下懒,第一次引用了element组件(纯JS也可以写的出来,赶时间嘛,懂得....),结果用了发现一个问题,element的组件( ...
- Application.Restore不起作用了
http://www.myexception.cn/delphi/695243.html Application.Restore不起作用了窗体上只有一个Button和一个Timer(1秒计时)代码如下 ...
- spring boot 启动之后404
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- 工具 - MSF
#ms17- use auxiliary/scanner/smb/smb_ms17_010 - exploit use exploit/windows/smb/ms17_010_eternalblue ...
- 一件很奇怪的事关于Nuget包
两个项目 A,B 分别引用了Newtonsoft.Json.dll A项目有引用B项目. A B 项目引用Newtonsoft.Json.dll文件源路径是相同的.但是经常报版本不匹配. 之后经过细心 ...
- C#建造者模式
/// <summary> /// 电脑类 /// </summary> public class Computer { //电脑组件集合 private IList<s ...
- Log4net使用(二)
转:http://www.cnblogs.com/basilwang/archive/2006/06/09/421868.html Log4net同时按照日期和大小生成RollingFile和访问Sy ...
- [Git] 005 初识 Git 与 GitHub 之分支
在 GitHub 的 UI 界面使用 Git(多图警告) 1. 建立分支 1.1 点击左上方的 Branch: master,在输入框中填入分支名,再点击下方的 Create branch 1.2 此 ...
- PHP 中的 $this, static , self ,parent 等等关键字的总结
先说结论: self 和 __CLASS__,都是对当前类的静态引用,取决于定义当前方法所在的类.也就是说,self 写在哪个类里面, 它引用的就是谁.$this 指向的是实际调用时的对象,也就是说, ...