[python-users] Try und Except
Christopher Arndt
chris.arndt at web.de
Do Apr 16 20:48:54 CEST 2015
Am 16.04.2015 um 20:04 schrieb Kay:
> try:
> int("hello")
> except Exception as exception:
> print(exception)
Als Ergänzung: man kann verschiedene Exceptions mit einem try-Block
abfangen:
try:
thing = open_thing(name)
except OSError as exc:
print("OS error tyring to open '%s': %s" % (name, exc)
except IOError as exc:
print("I/0 error tyring to open '%s': %s" % (name, exc)
except MemoryError:
print("Not enough memory.")
execpt:
print("Unexcpected error.")
else:
print("All fine and dandy.")
Von den Standard-Exceptions haben einige auch Standard-Instanzattribute,
die du dann in der except-Klausel untersuchen kannst. Z.B. haben IOError
und OSError die Attribute 'errno' und 'strerror'. Dies findest du hier
dokumentiert:
https://docs.python.org/2/library/exceptions.html
Chris
Mehr Informationen über die Mailingliste python-users