[Tutor] Problem with program ending

Kalle Svensson kalle@lysator.liu.se
Mon, 15 Jul 2002 23:49:29 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[a polite punk]
> My name is Jonathan and I am a new Python programmer!

Welcome to the list!

>  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:
> 
[code]
>    except:

The trouble is here.  When you use except without arguments, it
catches any exception.  sys.exit() raises an exception called
SystemExit, I think.

The thing to do is to just catch the exceptions you're expecting,
namely ValueError (from the int() call).

So, try replacing
    except:
with
    except ValueError:

This goes for all the other except: clauses as well.  except without
arguments should be used very rarely, because it can have unexpected
results (as you discovered).

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9M0NmdNeA1787sd0RAs/BAJ9Rx6ymMhfPjbUtSgFneJWEqRnX2gCgv/Qs
FvPc08uCZsMEK6QZx6LxRxQ=
=fKjw
-----END PGP SIGNATURE-----