[Tutor] Using sys.exit()

Luke Paireepinart rabidpoobear at gmail.com
Thu Nov 2 08:09:28 CET 2006


Dick Moores wrote:
> 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?
>
> 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
>   
Whenever an error is raised, IDEs catch the error and display it for you.
The SystemExit ended the process that was running your python script, 
just not the IDE.
So the IDE is around to display the error.
Imagine if whenever you tried to read in a file, and got an IOError, 
your IDE didn't catch it.
Wouldn't that be inconvenient?
The fact that it's called SystemExit doesn't change the fact that it's 
an exception.

'stripResponseAndCheckForUserRequestForHelpOrToQuit' is rather a long 
function name.
Perhaps you need more concise function names.
> If I can manage to use "break", all 3 exits are silent. Why is it 
> wrong to use "break" to exit?
>   
'break' doesn't exit.  It ends a loop.
It's not wrong to use a 'break' to exit a loop.  That's what it's there for.
But what if you were doing something after the loop?
'break' wouldn't help you there.
A SystemExit immediately ends the program at that call
(unless you catch the exception :)
example:
import random
a = random.randint(1,5)
i = 0
while i < 5:
    if i == a:
       break
    print "A is greater than %s" % i
print "A must be 5!"

See, if you were to raise a SystemExit there, then the final print 
statement wouldn't ever take place
unless a was actually 5.

The reason the exits with break are 'silent'
is because no exceptions are occurring.
SystemExit is an exception.
There's nothing wrong with using it.
You could raise an IOError if you wanted.

> Dick Moores
>
> Dick Moores
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   



More information about the Tutor mailing list