[Tutor] sys.exit help

Alan Gauld alan.gauld at btinternet.com
Mon Aug 2 02:20:51 CEST 2010


"Jason MacFiggen" <jmacfiggen at gmail.com> wrote

>I was wondering how can I change sys.exit so if you use command line 
>to run
> the program. it prompts a message asking if the user wants to exit 
> instead
> of automatically just exiting?

Don't just use sys.exit use a try/except SystemExit...
>>> import sys
>>> try:
...    sys.exit()
... except SystemExit:
...    print 'caught exit'
...    raise
...
caught exit

However thats usually the wrong way to do it.
Better to have your program exit naturally and have the control logic 
in
the code control when it finishes.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list