Re: delayed sys.exit?

2009-07-29 Thread Stephen Hansen

 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.


A bare except: catches ALL exceptions; including SystemExit which is
generated by
sys.exit. And KeyboardInterrupt, too. That's why its basically a bad
idea to use bare excepts unless you really, really, really need to.
Try 'except Exception' instead. SystemExit and such do not inherit
from Exception.

--S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: delayed sys.exit?

2009-07-29 Thread MRAB

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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: delayed sys.exit?

2009-07-29 Thread Peter Otten
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).

try:
   # If the conversion to int fails, nothing is appended to the list:
   Runs.append(int(strs[i]))
   if Runs[-1] = 0:
  print 'ERROR: ' + str(Runs[-i]) + \
' is invalid; run numbers must be positive.'
  sys.exit(0)
except:

sys.exit() works by raising a SystemExit exception which is caught by the 
bare except.

http://docs.python.org/library/sys.html#sys.exit
http://docs.python.org/library/exceptions.html#exceptions.SystemExit

As a general rule try to be as specific as possible when catching 
exceptions.

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list