正如你所看到的,即使程序应该已经死了,它也会从坟墓里说话。有没有办法在出现异常时“取消注册”exitfunction?
import atexit
def helloworld():
print("Hello World!")
atexit.register(helloworld)
raise Exception("Good bye cruel world!")
输出
Traceback (most recent call last):
File "test.py", line 8, in <module>
raise Exception("Good bye cruel world!")
Exception: Good bye cruel world!
Hello World!