您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中except:和except Exception之间的区别,例如e:

Python中except:和except Exception之间的区别,例如e:

在第二个中,您可以访问异常对象的属性

>>> def catch():
...     try:
...         asd()
...     except Exception as e:
...         print e.message, e.args
... 
>>> catch()
global name 'asd' is not defined ("global name 'asd' is not defined",)

但是它不会捕获BaseException或系统退出异常SystemExitKeyboardInterrupt并且GeneratorExit

>>> def catch():
...     try:
...         raise BaseException()
...     except Exception as e:
...         print e.message, e.args
... 
>>> catch()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in catch
BaseException

除了一个裸露的:

>>> def catch():
...     try:
...         raise BaseException()
...     except:
...         pass
... 
>>> catch()
>>>

有关更多信息,请参见文档的“内置异常”部分和本教程的“错误与异常”部分。

python 2022/1/1 18:28:03 有177人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶