Next: About this document Up: Recent Additions Previous: Miscellaneous New Built-in

Else Clause For Try Statement

The try...except statement now has an optional else clause, which must follow all except clauses. It is useful to place code that must be executed if the try clause does not raise an exception. For example:


        for arg in sys.argv:
                try:
                        f = open(arg, 'r')
                except IOError:
                        print 'cannot open', arg
                else:
                        print arg, 'has', len(f.readlines()), 'lines'
                        f.close()


guido@cwi.nl