First page Back Continue Last page Overview Image

Die optionale else-Klausel

arg = "yellow_snow.txt"

try:

f = open(arg, 'r')

except IOError:

print('cannot open', arg)

else:

print(arg, 'has', len(f.readlines()), 'lines')

f.close()

Die try ... except-Anweisung hat eine optionale else-Klausel, die nach allen except-Klauseln stehen muss. Dort befindet sich Code, der ausgeführt wird, wenn die try-Klausel keine Ausnahme auslöst.