CKTIS / Python
Keyword try
Protect a suite with matching exception handlers or attach unconditional cleanup with finally.
Version: Python 3.14; repository image, production runtime not attested
Syntax
try:
...
except ValueError:
...
else:
...
finally:
...Protect a suite with matching exception handlers or attach unconditional cleanup with finally.
A handled exception skips the remaining try statements and its else suite. Normal completion permits else, and finally runs after the chosen path.
Exceptions raised by an else suite are not caught by this statement’s preceding handlers.
Examples
def convert(s):
trace = []
try:
value = int(s)
except ValueError:
trace.append("invalid")
else:
trace.append(value)
finally:
trace.append("done")
return trace
print(convert("4"), convert("x"))
Result
[4, 'done'] ['invalid', 'done']
Exceptions raised by an else suite are not caught by this statement’s preceding handlers.