[Tutor] import sys; sys.exit()

Kent Johnson kent37 at tds.net
Thu Jan 10 19:35:42 CET 2008


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.

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
> 



More information about the Tutor mailing list