delayed sys.exit?

MRAB python at mrabarnett.plus.com
Wed Jul 29 16:41:37 EDT 2009


Dr. Phillip M. Feldman wrote:
> In the attached  http://www.nabble.com/file/p24726902/test.py test.py code,
> it appears that additional statements execute after the call to sys.exit(0). 
> I'll be grateful if anyone can shed light on why this is happening.  Below
> is a copy of some sample I/O.  Note that in the last case I get additional
> output after what should be the final error message.
> 
> In [126]: run test
> Input a string: 1,2
> [1, 2]
> 
> In [127]: run test
> Input a string: 1-3
> [1, 2, 3]
> 
> In [128]: run test
> Input a string: 1,4,5-12
> [1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
> 
> In [129]: run test
> Input a string: 0
> ERROR: 0 is invalid; run numbers must be positive.
> ERROR: '0' is not a valid run number.
> 
sys.exit raises an SystemExit exception, which then gets caught by the
bare 'except' of the enclosing try...except... statement.

A lot of things can raise an exception, which is why bare 'except's are
a bad idea; catch only those you expect.



More information about the Python-list mailing list