I knew that sys.exit() raises an Exit exception, so when I run this I knew it wouldn’t exit:
In [25]: try:
....: sys.exit()
....: except:
....: print "oops"
....:
oops
But I thought that os._exit() was meant to exit using a C call, but it’s also causing an exception:
In [28]: try:
....: os._exit()
....: except:
....: print "oops"
....:
oops
Is there a way of doing this without killing the PID?
Don’t use
exceptwithout an Exception class, sosys.exitwill just work fine without triggering the exception handling:There are other exceptions which are triggered with a plain except clause (and in general shouldn’t), especially
KeyboardInterrupt.