[Tutor] Problem with program ending

Sean 'Shaleh' Perry shalehperry@attbi.com
Fri, 12 Jul 2002 17:00:54 -0700 (PDT)


On 12-Jul-2002 a polite punk wrote:
> Hi,
> 
> My name is Jonathan and I am a new Python programmer!  But, I am having 
> trouble with a certain practice program that I have written.  It deals with 
> functions and the try and except statements.  The trouble is with ending the 
> program.  I use these commands below:
> 
>                          import sys
>                          sys.exit()
> 
> 
> 
> but when i do, it ignores it and continues with the program in the main 
> function.  I think it has to do with the use of the try and except 
> statements since i do not have this problem in the programs without the try 
> and except statements.  Here is the source code of the program below:
> 

>>> import sys
>>> try:
...   sys.exit(1)
... except:
...   print "caught it"
... 
caught it

you see, sys.exit() causes python to exit by raising an exception.

The problem with your code is you do not specify the type(s) of exception but
instead catch all of them.

>>> try:
...   sys.exit()
... except:
...   print sys.exc_type
... 
exceptions.SystemExit