Exceptions: Difference between revisions

Line 645:
try-except-finally-else
 
<lang python> try:
foo()
except SillyError, se:
print se.args
bar()
finally:
baz()
else:
# no exception occurred
quux()</lang>
 
Before Python 2.5 it was not possible to use finally and except together. (It was necessary to nest a separate ''try''...''except'' block inside of your ''try''...''finally'' block).
 
{{works with|Python|3.0}}
Note: Python3 will change the syntax of except slightly, but in a way that is not backwards compatible. In Python 2.x and earlier the ''except'' statement could list a single exception or a tuple/list of exceptions and optionally a name to which the exception object will be bound. In the old versions the exception's name followed a comma (as in the foregoing example). In Python3 the syntax will become: ''except Exception1 [,Exception2 ...] '''as''' ExceptionName''
<lang python>try:
foo()
except SillyError as se:
print(se.args)
bar()
finally:
baz()
else:
# no exception occurred
quux()</lang>
 
=={{header|Raven}}==
Anonymous user