<p>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?</p>

<pre><code>import atexit

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

atexit.register(helloworld)

raise Exception("Good bye cruel world!")
</code></pre>

<p>outputs</p>

<pre><code>Traceback (most recent call last):
  File "test.py", line 8, in &lt;module&gt;
    raise Exception("Good bye cruel world!")
Exception: Good bye cruel world!
Hello World!
</code></pre>

[{'answer_id': 81051, 'author': 'Joe Skora', 'author_id': 14057, 'author_profile': 'https://Stackoverflow.com/users/14057', 'pm_score': -1, 'selected': False, 'text': '<p>If you call</p>\n\n<pre><code>import os\nos._exit(0)\n</code></pre>\n\n<p>the exit handlers will not be called, yours or those registered by other modules in the application.</p>\n'}, {'answer_id': 81087, 'author': 'Sylvain Defresne', 'author_id': 5353, 'author_profile': 'https://Stackoverflow.com/users/5353', 'pm_score': 4, 'selected': True, 'text': '<p>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 <code>atexit</code> module.</p>\n\n<p>Something like that :</p>\n\n<pre><code>import sys\nimport atexit\n\ndef clear_atexit_excepthook(exctype, value, traceback):\n    atexit._exithandlers[:] = []\n    sys.__excepthook__(exctype, value, traceback)\n\ndef helloworld():\n    print "Hello world!"\n\nsys.excepthook = clear_atexit_excepthook\natexit.register(helloworld)\n\nraise Exception("Good bye cruel world!")\n</code></pre>\n\n<p>Beware that it may behave incorrectly if the exception is raised from an <code>atexit</code> registered function (but then the behaviour would have been strange even if this hook was not used).</p>\n'}, {'answer_id': 81107, 'author': 'Ber', 'author_id': 11527, 'author_profile': 'https://Stackoverflow.com/users/11527', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to calling os._exit() to avoid the registered exit handler you also need to catch the unhandled exception:</p>\n\n<pre><code>import atexit\nimport os\n\ndef helloworld():\n    print "Hello World!"\n\natexit.register(helloworld)    \n\ntry:\n    raise Exception("Good bye cruel world!")\n\nexcept Exception, e:\n    print \'caught unhandled exception\', str(e)\n\n    os._exit(1)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/80993', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15274/']