class abc(object):
def __enter__(self):
# 进入with 调用
pass
def __exit__(self, exc_type, exc_val, exc_tb):
# 离开with 调用
# 异常类型,异常值,异常追踪
print(exc_type)
print(exc_val)
print(exc_tb)
with abc() as a:
print("enter")
b = 1 / 0
print("exit")
分享改变世界,来自这里
Traceback (most recent call last):
File "/home/wjl/work_task/back_end/123.py", line 30, in <module>
b = 1 / 0
ZeroDivisionError: division by zero
enter
<class 'ZeroDivisionError'>
division by zero
<traceback object at 0x7fd55507a848>