How to Exit from Python Without Traceback

How to exit from Python without traceback?

You are presumably encountering an exception and the program is exiting because of this (with a traceback). The first thing to do therefore is to catch that exception, before exiting cleanly (maybe with a message, example given).

Try something like this in your main routine:

import sys, traceback

def main():
try:
do main program stuff here
....
except KeyboardInterrupt:
print "Shutdown requested...exiting"
except Exception:
traceback.print_exc(file=sys.stdout)
sys.exit(0)

if __name__ == "__main__":
main()

How do you exit a python program without the error statement?

You are trying too hard. Write your program using the regular boilerplate:

def main():
# your real code goes here
return

if __name__ == "__main__":
main()

and just return from function main. That will get you back to the if-clause, and execution will fall out the bottom of the program.

You can have as many return statements in main() as you like.

Why is sys.exit() causing a traceback?

You're running Python with the -i flag. -i suppresses the usual special handling of the SystemExit exception sys.exit raises; since the special handling is suppressed, Python performs the normal exception handling, which prints a traceback.

Arguably, -i should only suppress the "exit" part of the special handling, and not cause a traceback to be printed. You could raise a bug report; I didn't see any existing, related reports.

Print an error message without printing a traceback and close the program when a condition is not met

You can use a try: and then except Exception as inst:
What that will do is give you your error message in a variable named inst and you can print out the arguments on the error with inst.args. Try printing it out and seeing what happens, and is any item in inst.args is the one you are looking for.

EDIT Here is an example I tried with pythons IDLE:

>>> try:
open("epik.sjj")
except Exception as inst:
d = inst

>>> d
FileNotFoundError(2, 'No such file or directory')
>>> d.args
(2, 'No such file or directory')
>>> d.args[1]
'No such file or directory'
>>>

EDIT 2: as for closing the program you can always raise and error or you can use sys.exit()

How to exit from a python program without using libraries and modules

try to use:

exit("some message")

it is a built in function

Python CTRL-C exit without traceback?

You call play() twice, so you need to put both cases in a try/except block:

if next in ("yes", "y"):
try:
play()
except KeyboardInterrupt:
print "Goodbye!"
except Exception:
traceback.print_exc(file=sys.stdout)
sys.exit(0)
elif next is None:
try:
play()
except KeyboardInterrupt:
print "Goodbye!"
except Exception:
traceback.print_exc(file=sys.stdout)
sys.exit(0)
else:
sys.exit(0)

I've corrected two other problems, it is better to test for None with is in python, and your first if test was not going to work, as next == "yes" or "y" is interpreted as next == "yes" separately from "y" with an or in between. The "y" is always seen as True so you never would come to the other branches in your code.

Note that I suspect the above code could be simplified much more, but you don't show us your play() function at all, so you leave us to guess what you are trying to do.

Stop Python code without an error

As JBernardo pointed out, sys.exit() raises an exception. This exception is SystemExit. When it is not handled by the user code, the interpreter exits cleanly (a debugger debugging the program can catch it and keep control of the program, thanks to this mechanism, for instance)—as opposed to os._exit(), which is an unconditional abortion of the program.

This exception is not caught by except Exception:, because SystemExit does not inherit from Exception. However, it is caught by a naked except: clause.

So, if your program sees an exception, you may want to catch fewer exceptions by using except Exception: instead of except:. That said, catching all exceptions is discouraged, because this might hide real problems, so avoid it if you can, by making the except clause (if any) more specific.

My understanding of why this SystemExit exception mechanism is useful is that the user code goes through any finally clause after a sys.exit() found in an except clause: files can be closed cleanly, etc.; then the interpreter catches any SystemExit that was not caught by the user and exits for good (a debugger would instead catch it so as to keep the interpreter running and obtain information about the program that exited).



Related Topics



Leave a reply



Submit