发生未处理的异常时如何跳过 sys.exitfunc

发布于 2024-07-04 06:28:23 字数 427 浏览 13 评论 0原文

正如您所看到的,即使程序应该已经死亡,它仍然会从坟墓中说话。 有没有办法在出现异常时“取消注册”退出函数?

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!

As you can see, even after the program should have died it speaks from the grave. Is there a way to "deregister" the exitfunction in case of exceptions?

import atexit

def helloworld():
    print("Hello World!")

atexit.register(helloworld)

raise Exception("Good bye cruel world!")

outputs

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-07-11 06:28:23

除了调用 os._exit() 以避免注册退出处理程序之外,您还需要捕获未处理的异常:

import atexit
import os

def helloworld():
    print "Hello World!"

atexit.register(helloworld)    

try:
    raise Exception("Good bye cruel world!")

except Exception, e:
    print 'caught unhandled exception', str(e)

    os._exit(1)

In addition to calling os._exit() to avoid the registered exit handler you also need to catch the unhandled exception:

import atexit
import os

def helloworld():
    print "Hello World!"

atexit.register(helloworld)    

try:
    raise Exception("Good bye cruel world!")

except Exception, e:
    print 'caught unhandled exception', str(e)

    os._exit(1)
人生戏 2024-07-11 06:28:23

我真的不知道你为什么要这样做,但是你可以安装一个 excepthook,每当引发未捕获的异常时,Python 都会调用它,并清除 atexit 中注册函数的数组。 > 模块。

类似这样的事情:

import sys
import atexit

def clear_atexit_excepthook(exctype, value, traceback):
    atexit._exithandlers[:] = []
    sys.__excepthook__(exctype, value, traceback)

def helloworld():
    print "Hello world!"

sys.excepthook = clear_atexit_excepthook
atexit.register(helloworld)

raise Exception("Good bye cruel world!")

请注意,如果从 atexit 注册函数引发异常,它可能会表现不正确(但即使不使用此钩子,行为也会很奇怪)。

I don't really know why you want to do that, but you can install an excepthook that will be called by Python whenever an uncatched exception is raised, and in it clear the array of registered function in the atexit module.

Something like that :

import sys
import atexit

def clear_atexit_excepthook(exctype, value, traceback):
    atexit._exithandlers[:] = []
    sys.__excepthook__(exctype, value, traceback)

def helloworld():
    print "Hello world!"

sys.excepthook = clear_atexit_excepthook
atexit.register(helloworld)

raise Exception("Good bye cruel world!")

Beware that it may behave incorrectly if the exception is raised from an atexit registered function (but then the behaviour would have been strange even if this hook was not used).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文