[Tutor] Using sys.exit()

Andreas Kostyrka andreas at kostyrka.org
Thu Nov 2 10:45:25 CET 2006


Am Mittwoch, den 01.11.2006, 22:16 -0800 schrieb Dick Moores:
> At 03:56 PM 11/1/2006, Andreas Kostyrka wrote:
> >Am Mittwoch, den 01.11.2006, 15:43 -0800 schrieb Dick Moores:
> > > At 12:14 AM 10/31/2006, Alan Gauld wrote:
> > >
> > > >"Dick Moores" <rdm at rcblue.com> wrote
> > > > > I'd like to know how to use sys.exit() to quit a program.
> > > > >
> > > >
> > > >I see that you already figured that out.
> > > >You can also quit by raising SystemExit, which is
> > > >what sys.exit does... but you don't need to import sys...
> > >
> > > I'm afraid I don't know what you mean. How do I raise SystemExit, and
> > > why don't I need to import sys?
> >
> >raise SystemExit(2)
> >
> >is equal to sys.exit(2) (actually sys.exit(2) just raises SystemExit(2))
> 
> OK, that works well. But why the 2?

2 is the usual error code for invalid cmdline parameters in the Unix
world. Just an example here.

> 
> BTW at the command line, "raise SystemExit(2)" produces a completely 
> silent exit. In Win IDE I get "SystemExit: 2". With IDLE:
> 
> Traceback (most recent call last):
>    File "E:\Python25\dev\1unitConversion5a.py", line 425, in <module>
>      main()
>    File "E:\Python25\dev\1unitConversion5a.py", line 413, in main
>      s = formatAndCheckStringFromUser(s)
>    File "E:\Python25\dev\1unitConversion5a.py", line 342, in 
> formatAndCheckStringFromUser
>      s = stripResponseAndCheckForUserRequestForHelpOrToQuit(s)
>    File "E:\Python25\dev\1unitConversion5a.py", line 253, in 
> stripResponseAndCheckForUserRequestForHelpOrToQuit
>      raise SystemExit(2)
> SystemExit: 2
> 
> If I can manage to use "break", all 3 exits are silent. Why is it 
> wrong to use "break" to exit?

Because it does not:

for i in xrange(50):
    for j in xrange(50, 100):
        if j == 77:
            break

will not stop the program when it hits the break.

But yes, there are no reason why you should not let your script just
end.

Andreas

> 
> Dick Moores
> 
> Dick Moores
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20061102/477eabbf/attachment-0001.pgp 


More information about the Tutor mailing list