Python-da Istisnalarin Yoxlanilmasi Sertlerinde Istifadeci Istisnalari
Istisna atmag adeten xususi sertlerle temin edilir.
Tutaq ki, bir siyahimiz var:
lst = [1, 2, 3]
Gelin sert qoyaq, eger siyahinin uzunlugu
uc ve ya daha kicikdirse, elementlerin cemi
hesablansin. Eks halda ise
else blokunda oz istisnamiz
ToBigLength atilsin:
try:
if len(lst) <= 3:
print(sum(lst))
else:
raise ToBigLength
except ToBigLength:
print('error: list is too big')
Kodu icra etdikden sonra cixacaq:
6
Indi gelin siyahidaki elementlerin sayini artiraq:
lst = [1, 2, 3, 4]
try:
if len(lst) <= 3:
print(sum(lst))
else:
raise ToBigLength
except ToBigLength:
print('error: list is too big')
Kodu icra etdikden sonra cixacaq:
'error: list is too big'
Bir sert yazin, eger eded musbetdirse, onun kvadrati hesablansin. Eger eded menfidirse - evvelki ders ucun yaratdiginiz istisna atilsin.
Bir sert yazin, eger eded sifra beraber deyilse,
ona 5 elave edilsin. Eks
teqdirde evvelki ders ucun yaratdiginiz
istisna atilsin.