Python probeer Sleutelwoord
Voorbeeld
Probeer 'n blok kode en besluit wat om te doen as dit 'n fout veroorsaak:
try:
x > 3
except:
print("Something went wrong")
Definisie en gebruik
Die try
sleutelwoord word gebruik in probeer ... behalwe blokke. Dit definieer 'n blok kode toets as dit enige foute bevat.
Jy kan verskillende blokke vir verskillende fouttipes definieer, en blokke om uit te voer as niks verkeerd geloop het nie, sien voorbeelde hieronder.
Meer voorbeelde
Voorbeeld
Stel 'n fout op en stop die program wanneer daar 'n fout in die probeerblok is:
try:
x > 3
except:
Exception("Something went wrong")
Verwante bladsye
Die except
sleutelwoord.
Die finally
sleutelwoord.