首页 > 文章列表 > python with遇到错误语句的处理

python with遇到错误语句的处理

Python with
198 2022-08-07

说明

1、将错误的类型、值和回溯传递给_exit__方法。

2、允许__exit__方法处理异常。

3、如果__exit__返回,True将得到妥善处理。

如果True不返回任何其他内容(None),__exit__with语句会引起异常。

实例

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'file' object has no attribute 'undefined_function'

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。