在python文档中,SystemExit不是Exception类的子类。BaseException类是SystemExit的基类。因此,在给定的代码中,我们将Exception替换为BaseException以使代码正常工作
try: raise SystemExit except BaseException: print "有用!"
输出结果
有用!
异常继承自BaseException,而不是StandardError或Exception,因此不会被捕获Exception的代码意外捕获。
我们宁愿这样写代码
try: raise SystemExit except SystemExit: print "有用!"
输出结果
有用!