Python-da Istisnalarin Yoxlanilmasi Sertlerinde Istifadeci Istisnalari

Istisna atmag adeten xususi sertlerle temin edilir.

Tutaq ki, bir siyahimiz var:

pythonlst = [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:

pythontry:
  if len(lst) <= 3:
    print(sum(lst))
  else:
    raise ToBigLength
except ToBigLength:
  print('error: list is too big')

Kodu icra etdikden sonra cixacaq:

python6

Indi gelin siyahidaki elementlerin sayini artiraq:

pythonlst = [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:

python'error: list is too big'
Задание 1

Bir sert yazin, eger eded musbetdirse, onun kvadrati hesablansin. Eger eded menfidirse - evvelki ders ucun yaratdiginiz istisna atilsin.

Задание 2

Bir sert yazin, eger eded sifra beraber deyilse, ona 5 elave edilsin. Eks teqdirde evvelki ders ucun yaratdiginiz istisna atilsin.