[Tutor] Two sys.exit questions

Luke Paireepinart rabidpoobear at gmail.com
Wed Feb 28 19:07:13 CET 2007


Cecilia Alm wrote:
> I have two quick questions:
>
> 1) Why does sys.exit() not work in a try clause (but it does in the 
> except clause)?
sys.exit raises an exception.  That's how it exits program execution.
If you use it in a try block, the exception it raises will have no 
effect because your except clause isn't catching
a particular kind of error, it's catching all errors (including 
"SystemExit" ones)

an example of a sys.exit working in a try block would be something like 
this:

import sys
try:
   f = file('somefile_you_have.txt','r')
   sys.exit(0)

except IOError:
   print "You had an error on file input"


hope that clears some things up for you.
-Luke


More information about the Tutor mailing list