[Tutor] Two sys.exit questions

Cecilia Alm flickita at gmail.com
Wed Feb 28 18:49:14 CET 2007

I have two quick questions:

1) Why does sys.exit() not work in a try clause (but it does in the except

>>> try:
...    print 1
...    sys.exit(0)
... except:
...    print 2
...    sys.exit(0)
# python exited

2) If opening a file fails in the below 2 cases, sys.exit(message) prints a
message in the except clause before program termination.
    Some use file.close() in the except clause (or in a finally clause). It
seems superflous in the below case of read and write. (?)

            file = open('myinfile.txt', 'r')
        except IOError:
            sys.exit('Couldn't open myinfile.txt')

            file = open('myoutfile.txt', 'w')
        except IOError:
            sys.exit('Couldn't open myoutfile.txt')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070228/96405c6b/attachment-0001.htm 

More information about the Tutor mailing list