[Tutor] import sys; sys.exit()

Eric Brunson brunson at brunson.com
Thu Jan 10 22:22:02 CET 2008


Kent Johnson wrote:
> Andrew Volmensky wrote:
>
>   
>> ...and get an error. I have tried saving as exit.py from the editor  
>> and trying to run it and also entering the commands directly into the  
>> shell. My understanding is that this is supposed to exit the program,  
>> but that does not appear to be happening.
>>     
>
> sys.exit() doesn't do what you might think - it doesn't force an exit 
> from the current process. All it does is raise a SystemExit exception. 
> How this is handled depends on the context - SystemExit can be caught by 
> an enclosing except handler just like any other exception.
>
> If you are running a program directly, SystemExit will probably 
> propagate to the interpreter and cause the program to exit, just as any 
> other uncaught exception would.
>
> Running in IDLE or another shell, the exception is caught and logged but 
> it doesn't abort the process.
>   


FWIW, and in no way contradictory to what Kent said above, I find it 
more pythonic to simply raise SystemExit.  It doesn't require an import 
and when used in a program, if you raise SystemExit() with no 
parameters, the program will exit with no error.  Raising SystemExit( 
'some error message' ) will print the error message and exit with a 
non-zero status.


> Kent
>
>   
>> This is with MacPython 2.4 - Thanks!
>>
>> IDLE 1.1.4
>>  >>> ================================ RESTART  
>> ================================
>>  >>>
>>
>> Traceback (most recent call last):
>>    File "/Users/andrew/python/exit.py", line 2, in -toplevel-
>>      sys.exit( )
>> SystemExit
>>
>>
>>  >>> import sys
>>  >>> sys.exit()
>>
>> Traceback (most recent call last):
>>    File "<pyshell#1>", line 1, in -toplevel-
>>      sys.exit()
>> SystemExit
>>  >>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>     
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list