Python包装异常处理方法
作者:生而为人我很遗憾 时间:2022-03-13 12:08:42
前言
相比java,python的异常和java中不同,python主要是防止程序异常被中止。一旦被catch后它还行往下执行。
一、异常
1.1、忽略
pass这个关键字相当于一个占位符,好比TODO是一样的,只表示此行什么也不做,不代表其它的行代码不执行;
try:
print(5/0)
except ZeroDivisionError:
pass
print("ddd") #这行还是可以正常执行的
1.2、捕获
def parse_int(s):
try:
n = int(v)
except Exception as e:
print('Could not parse, Reason:', e)
parse_int('30') ##Reason: name 'v' is not defined
1.3、异常链
try:
client_obj.get_url(url)
except (URLError, ValueError, SocketTimeout):
client_obj.remove_url(url)
try:
client_obj.get_url(url)
except (URLError, ValueError):
client_obj.remove_url(url)
except SocketTimeout:
client_obj.handle_url_timeout(url)
try:
f = open(filename)
except OSError:
pass
1.4、自定义
class NetworkError(Exception):
pass
class HostnameError(NetworkError):
pass
class CustomError(Exception):
def __init__(self, message, status):
super().__init__(message, status)
self.message = message
self.status = status
try:
msg = s.recv()
except TimeoutError as e:
print(e)
except RuntimeError as e:
print(e.args)
1.5、抛出
try:
raise RuntimeError('It failed') #抛出新异常-raise Error
except RuntimeError as e:
print(e.args)
def example():
try:
int('N/A')
except ValueError:
print("Didn't work")
raise #捕获后再抛出
二、异常的显示方式
2.1、打印信息
try:
print(5/0)
except ZeroDivisionError as e:
print(e.args)
2.2、控制台警告
import warnings
warnings.simplefilter('always')
def func(x, y, log_file=None, debug=False):
if log_file is not None:
warnings.warn('log_file argument deprecated', DeprecationWarning)
func(1, 2, 'a')
#第一行日志输出warn内容,第二行输出代码内容
/Users/liudong/personCode/python/pythonTest/app/base/base_type.py:5: UserWarning: log_file argument deprecated
warnings.warn('log_file argument deprecated')
2.2、存储文件
import json;
numbers = [2,3,4,5,6];
fileName = "numbers.json";
with open(fileName, "w") as fileObj:
json.dump(numbers, fileObj);
with open(fileName, "r") as fileObj:
number1 = json.load(fileObj);
来源:https://blog.51cto.com/arch/5397837
标签:Python,包装,异常,处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分享个asp文件缓存代码,使程序从缓存读数据
2011-03-09 19:47:00
PyQt5的相对布局管理的实现
2022-02-23 08:11:18
![](https://img.aspxhome.com/file/2023/9/86199_0s.png)
pandas is in和not in的使用说明
2023-04-04 22:06:11
![](https://img.aspxhome.com/file/2023/7/117377_0s.jpg)
Python文件操作之合并文本文件内容示例代码
2021-06-24 13:58:05
![](https://img.aspxhome.com/file/2023/5/92315_0s.png)
对Python3 解析html的几种操作方式小结
2021-04-07 20:13:12
asp如何做一个密码“生成器”?
2010-07-12 18:51:00
在 SQL Server数据库开发中的十大问题
2009-01-20 14:56:00
表单制作方式大比拼
2008-10-09 11:32:00
![](https://img.aspxhome.com/file/uploadpic/20071/2007131102013522.gif)
python学生管理系统代码实现
2023-10-31 07:55:04
使用Python通过win32 COM打开Excel并添加Sheet的方法
2021-12-07 11:53:34
![](https://img.aspxhome.com/file/2023/5/114625_0s.jpg)
端午节将至,用Python爬取粽子数据并可视化,看看网友喜欢哪种粽子吧!
2023-08-23 06:29:31
![](https://img.aspxhome.com/file/2023/2/62272_0s.png)
设置密码保护的SqlServer数据库备份文件与恢复文件的方法
2011-11-03 16:55:30
jupyter notebook中美观显示矩阵实例
2023-06-06 18:13:35
![](https://img.aspxhome.com/file/2023/0/87560_0s.jpg)
利用ASP在线维护数据库
2007-10-12 13:53:00
![](https://img.aspxhome.com/file/UploadPic/200710/12/20071012135842950s.jpg)
js和jquery判断数据类型的4种方法总结
2023-08-25 08:49:18
asp之自动闭合HTML/ubb标签函数+简单注释
2008-09-29 12:21:00
详解利用Python制作中文汉字雨效果
2023-04-26 07:09:17
![](https://img.aspxhome.com/file/2023/3/102973_0s.jpg)
游戏的用户体验营销小札
2009-08-30 15:13:00
python爬取网页版QQ空间,生成各类图表
2023-01-18 06:30:58
![](https://img.aspxhome.com/file/2023/5/69025_0s.png)
页面加载对访问的影响
2009-10-30 18:54:00
![](https://img.aspxhome.com/file/UploadPic/200910/30/speed_taobaoued-30s.png)