Why is else clause needed for try statement in python? [duplicate]
问题 This question already has answers here : Python try-else (19 answers) Closed 5 years ago . In Python the try statement supports an else clause, which executes if the code in try block does not raise an exception. For example: try: f = open('foo', 'r') except IOError as e: error_log.write('Unable to open foo : %s\n' % e) else: data = f.read() f.close() Why is the else clause needed? Can't we write the above code as follows : try: f = open('foo', 'r') data = f.read() f.close() except IOError as