python自定义异常处理with,上下文context管理器
一个简单的异常处理,只打印异常信息,不raise异常: https://blog.csdn.net/wifeisboss/article/details/47316117
# 定义with上下文管理器
@contextlib.contextmanager
def context_manager():
try:
yield
except (Exception) as ex:
print(ex)
# 执行代码
with context_manager():
1/0
# 输出
> integer division or modulo by zero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
这篇文章讲的不错: https://blog.csdn.net/IAlexanderI/article/details/81027798
这篇文章讲的很全: https://blog.csdn.net/weixin_34192993/article/details/88112427
编辑 (opens new window)
上次更新: 2022/04/24, 14:15:57