Consider the following example:
try:
print "Try this."
exit(0)
except:
print "Failed."
exit(1)
When this simple example is executed, the following is printed to stdout:
Try this.
Failed.
Forgive me if there is an obvious answer or if this has already been asked, but why does exit(0)
(which should give me a clean exit) throw an exception?
I tried using a try: except:
loop in an actual use-case script, and found that the script would exit(1) even when the try
condition says to exit(0).
So I guess exit(0)
is throwing a(n hidden?) exception of some sort, but why? It doesn't show the traceback (such as when CTRL + C is used to generate a KeyboardInterrupt exception), and results in unexpected behavior such as in my case when I'm expecting a 0 return and end up with a 1 return.
Is an exception (any exception) the only way to exit a python script?