如何在 Python 中捕获 SystemExit 异常?
在 Python 文档中,SystemExit 不是 Exception 类的子类。BaseException 类是 SystemExit 的基类。因此在给定的代码中,我们用 BaseException 替换 Exception 以使代码正常工作
示例
try: raise SystemExit except BaseException: print "It works!"
输出
It works!
异常从 BaseException 继承,而不是 StandardError 或 Exception,因此不会意外被捕获到捕获 Exception 的代码中。
我们希望通过这种方式编写代码
示例
try: raise SystemExit except SystemExit: print "It works!"
输出
It works!
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP