Posted By

ManishChandraReddy on 12/13/15


Tagged

error Handling


Versions (?)

Try Except Else


 / Published in: Python
 

URL: https://docs.python.org/2/tutorial/errors.html

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

  1. for arg in sys.argv[1:]:
  2. try:
  3. f = open(arg, 'r')
  4. except IOError:
  5. print 'cannot open', arg
  6. else:
  7. print arg, 'has', len(f.readlines()), 'lines'
  8. f.close()

Report this snippet  

You need to login to post a comment.