Skip to content
English

Errors

Reading an error message

When a program cannot go on, Algorizm stops and prints a message in Uzbek. The message gives the line number and, if the error happened inside functions, the chain of calls that led there.

funksiya uchinchi(x):
    qaytar 10 / x

funksiya ikkinchi(x):
    qaytar uchinchi(x - 1)

funksiya birinchi():
    chiqar("boshlandi")
    qaytar ikkinchi(1)

birinchi()
chiqar("bu qator chiqmaydi")
Output
boshlandi
Xato (2-qator): Nolga bo'lish mumkin emas
Chaqiruvlar zanjiri:
  xato_zanjir.alg, 2-qator: 'uchinchi' funksiyasida
  xato_zanjir.alg, 5-qator: 'ikkinchi' funksiyasida
  xato_zanjir.alg, 9-qator: 'birinchi' funksiyasida
  xato_zanjir.alg, 11-qator

Read it from the top: the first line says what went wrong and on which line. Chaqiruvlar zanjiri (the chain of calls) then shows how the program got there, from the function where the error happened back to the line of the main program that started it.

There are two kinds of message:

  • Sintaksis xatosi (syntax error): the program text is not written correctly. It is found before the program starts, so nothing has run yet.
  • Xato (error): something went wrong while the program was running, such as dividing by zero or using a name that does not exist.

Hints

Algorizm tries to say how to fix a mistake. If a name is not known but resembles one that is, it suggests it:

narxlar = [12000, 25000, 8000]
chiqar(uzunlk(narxlar))
Output
Xato (2-qator): 'uzunlk' aniqlanmagan. Balki 'uzunlik' demoqchimisiz?

If an English keyword or a Python function was typed out of habit, it names the Uzbek word:

print("salom")
Output
Xato (1-qator): 'print' aniqlanmagan. Algorizmda 'print' o'rniga 'chiqar' yoziladi
x = 5
if x > 3:
    chiqar("katta")
Output
Sintaksis xatosi (2-qator): Qator oxiri kutilgan edi, ammo 'x' topildi. Algorizmda 'if' o'rniga 'agar' yoziladi

Catching errors: urin and ushla

An error does not have to stop the program. Put the statements that may fail into an urin block. If an error happens there, the rest of the block is skipped and the ushla block runs. The name after ushla receives the error.

funksiya bolish(a, b):
    urin:
        qaytar a / b
    ushla xato:
        chiqar("Xato:", xato)
        qaytar 0

chiqar(bolish(10, 4))
chiqar(bolish(10, 0))
Output
2.5
Xato: Nolga bo'lish mumkin emas
0

An error raised by Algorizm itself arrives as text, the same message that would have been printed. An error also travels out of functions: if it is not caught where it happens, it goes to the caller, and so on, until some urin catches it or the program ends.

Some messages you are likely to meet:

funksiya sinab(ish):
    urin:
        ish()
    ushla xato:
        chiqar(xato)

sinab(funksiya(): [1, 2][5])
sinab(funksiya(): {"a": 1}["b"])
sinab(funksiya(): "5" + 5)
sinab(funksiya(): butun("abc"))
sinab(funksiya(): uzunlik(5))
Output
Indeks chegaradan tashqarida: 5 (ro'yxat uzunligi 2)
Lug'atda bunday kalit yo'q: "b"
Matn bilan butun turini '+' orqali qo'shib bo'lmaydi. matn(...) funksiyasidan yoki f"...{qiymat}..." yozuvidan foydalaning
"abc" matnini butun songa aylantirib bo'lmaydi
butun turidagi qiymatning uzunligi yo'q

Raising errors: tashla

tashla raises an error of your own. It is the right way for a function to report that it was given something it cannot work with. Any value can be thrown: usually a text, but also a dictionary or an object that carries more detail.

funksiya yosh_tekshir(yosh):
    agar yosh < 0:
        tashla "Yosh manfiy bo'lishi mumkin emas"
    qaytar yosh

urin:
    yosh_tekshir(-5)
ushla xato:
    chiqar("Ushlandi:", xato)

urin:
    tashla {"kod": 404, "xabar": "topilmadi"}
ushla x:
    chiqar(x["kod"], x["xabar"])
Output
Ushlandi: Yosh manfiy bo'lishi mumkin emas
404 topilmadi

An error that is thrown and not caught stops the program, and its value is printed as the message.

Good to know

  • Catch an error where the program can do something sensible about it, such as asking again or using a default. Otherwise let it stop the program: the message is the most useful thing a programmer can get.
  • Files and folders that are missing, text that is not a number, a key that is not in a dictionary: all of these are errors that urin can catch.
  • A program that ends because of an error returns exit code 1 to the system; a program that ends normally returns 0.

Next

Modules shows how to split a program into files and use the standard library.