Python异常与错误处理详细讲解
作者:soapcmd 时间:2022-07-07 05:13:38
基础知识
优先使用异常捕获
LBYL(look before you leap): 在执行一个可能出错的操作时,先做一些关键的条件判断,仅当满足条件时才进行操作。
EAFP(eaiser to ask for forgiveness than permission): 不做事前检查,直接执行操作。
后者更优: 代码简洁,效率更高
try语句常用知识
把更精确的except语句放在前面
异常类派生关系: BaseException --> Exception --> LookupError --> KeyError
父类被捕获后子类就不会再被触发
使用else分支
try except else
else: 仅当try语句块里面没有抛出任何异常时,才执行else分支
和finally不同,假如在try语句块时碰到了return或者break, 中断了本次异常,那么即使代码没抛出任何异常,else分支内的逻辑也不会被执行
而finally里的语句,无论如何都会被执行,哪怕已经执行了return
使用空raise语句
>>> def incr_by_key(d, key):
... try:
... d[key] += 1
... except KeyError:
... print('here')
... raise
...
>>> d = {'a': 1}
>>> incr_by_key(d, 'b')
here
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in incr_by_key
KeyError: 'b'
>>> d['c'] += 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'c'
当一个空raise语句出现在except块里时,它会原封不动地重新抛出当前异常
抛出异常,而不是返回错误
使用上下文管理器
__enter__
__exit__
>>> class DummyContext:
... def __init__(self, name):
... self.name = name
... def __enter__(self):
... return f"{self.name} -- something"
... def __exit__(self, exc_type, exc_val, exc_db):
... print("Exiting")
... return False
...
>>> with DummyContext('foo') as name:
... print(f'Name: {name}')
...
Name: foo -- something
Exiting
用于替代finally 语句清理资源
在__exit__
里面清理资源。
此外__exit__
也可以用来对异常进行二次处理然后抛出,或是忽略某种异常等等。
用户忽略异常
一般可以捕获异常后pass
但是也可以:
def __exit__(self, exc_type, exc_val, exc_db):
if exc_type == SomeException:
return True
return False
此外:使用contextlib里面的suppress也可以实现相同的功能
使用contextmanage装饰器
>>> @contextmanager
... def create_con_obj(host, port, timeout=None):
... conn = create_conn(host, port, timeout=timeout)
... try:
... yield conn
... finally:
... conn.close()
yield前面的语句会在进入管理器时执行(类似:__enter__
)
之后的逻辑会在退出管理器时执行(类似:__exit__
)
来源:https://blog.csdn.net/weixin_44596902/article/details/128270006
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL安全性指南(3)(转)
python 将字符串中的数字相加求和的实现
![](https://img.aspxhome.com/file/2023/4/66004_0s.png)
微信小程序如何调用图片接口API并居中显示
![](https://img.aspxhome.com/file/2023/1/56021_0s.jpg)
微信小程序使用Vant Weapp组件库的方法步骤
![](https://img.aspxhome.com/file/2023/6/56286_0s.jpg)
Python selenium如何设置等待时间
Python读取图片为16进制表示简单代码
![](https://img.aspxhome.com/file/2023/1/71381_0s.png)
Pivot,信息组织的梦想之窗
![](https://img.aspxhome.com/file/UploadPic/20101/11/pivot-01-87s.jpg)
Python10行代码实现模拟百度搜索的示例
![](https://img.aspxhome.com/file/2023/4/70844_0s.jpg)
PHP Session条件竞争超详细讲解
![](https://img.aspxhome.com/file/2023/7/55337_0s.png)
ie7.0浏览器 兼容问题苦煞网站设计者
使用python tkinter开发一个爬取B站直播弹幕工具的实现代码
![](https://img.aspxhome.com/file/2023/5/81495_0s.png)
用asp判断某IP是否属于某网段的另类算法
关于web标准的思考
Python源码学习之PyType_Type和PyBaseObject_Type详解
![](https://img.aspxhome.com/file/2023/3/61733_0s.png)
python提取照片坐标信息的实例代码
Python读取hdf文件并转化为tiff格式输出
![](https://img.aspxhome.com/file/2023/2/77732_0s.png)
用css+js给网页背景插入flash播放器
Django框架基础认证模块auth应用示例
![](https://img.aspxhome.com/file/2023/0/64240_0s.png)
python科学计算之numpy——ufunc函数用法
![](https://img.aspxhome.com/file/2023/8/70828_0s.jpg)