All sys.exit()
does is raise an exception of type SystemExit
.
From the documentation:
Exit from Python. This is implemented by raising the
SystemExit
exception, so cleanup actions specified byfinally
clauses oftry
statements are honored, and it is possible to intercept the exit
attempt at an outer level.
If you run the following, you’ll see for yourself:
import sys
try:
sys.exit(0)
except SystemExit as ex:
print 'caught SystemExit:', ex
As an alternative, os._exit(n)
with the status code will stop the process bypassing much of the cleanup, including finally
blocks etc.