CKTIS / Python
Parola chiave except
Gestisce la prima clausola compatibile con il tipo dell’eccezione o una base.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
except (ValueError, TypeError) as error:
...Gestisce la prima clausola compatibile con il tipo dell’eccezione o una base.
Una tupla raggruppa alternative e il nome dopo as viene cancellato alla fine per spezzare cicli. Exception esclude SystemExit e KeyboardInterrupt. Python 3.14 accetta più tipi senza parentesi in assenza di as. except* divide un gruppo di eccezioni in sottogruppi compatibili.
Uno stesso try non può mescolare except ed except*.
Esempi
try:
int("blue")
except (ValueError, TypeError) as err:
print(type(err).__name__)
print("err" in globals())
try:
raise TypeError("units")
except ValueError, TypeError:
print("group")
try:
raise ExceptionGroup("two", [ValueError(), TypeError()])
except* ValueError as part:
print("value", len(part.exceptions))
except* TypeError as part:
print("type", len(part.exceptions))
Risultato
ValueError False group value 1 type 1
Uno stesso try non può mescolare `except` ed `except*`.