7. When things go wrong¶
Programs deal with an imperfect world: a user who didn't give an email address, a file that isn't there, a number that isn't valid. This lesson is about handling that calmly.
عدم: when there is no value¶
عدم means «nothing». In Ḍād, a name only accepts it if you say so explicitly, with ؟
after the type:
نوع مستخدم(الاسم: نص، البريد: نص؟ = عدم)
ثابت هدى = مستخدم("هدى"، البريد: "huda@example.com")
ثابت سالم = مستخدم("سالم")
اطبع(هدى.البريد، سالم.البريد)
نص؟ means «text, or nothing». So the type alone tells you a value might be missing
(here, a user's email, البريد), and you aren't caught by surprise.
؟. and ؟؟¶
نوع مستخدم(الاسم: نص، البريد: نص؟ = عدم)
دالة عرض_البريد(م: مستخدم): نص = م.البريد ؟؟ "(بلا بريد)"
دالة طول_البريد(م: مستخدم؟): صحيح = م؟.البريد؟.طول ؟؟ 0
اطبع(عرض_البريد(مستخدم("هدى"، البريد: "huda@example.com")))
اطبع(عرض_البريد(مستخدم("سالم")))
اطبع(طول_البريد(مستخدم("هدى"، البريد: "huda@example.com"))، طول_البريد(عدم))
أ ؟؟ ب: takeأ, and if it's nothing, takeب.م؟.البريد: ifمis nothing, the result is nothing; otherwise, its email. That way the program doesn't stop when you ask «nothing» for something.
Errors while running¶
Some mistakes only show up while the program runs, such as asking a list of three for its tenth item. The program then stops and tells you where and why:
The message gives the line and column, and says the index 10 is out of bounds (the length is 3). The last line («this won't be printed») never runs.
حاول and التقط¶
When you expect that something might fail, put it in حاول («try»), and say what happens if
it fails in التقط («catch»). Here, reading an age from text:
دالة اقرأ_العمر(النص: نص): صحيح {
ثابت العمر = حاول { النص ك صحيح } التقط خ { -1 }
أرجع العمر
}
اطبع(اقرأ_العمر("25"))
اطبع(اقرأ_العمر("خمسة وعشرون"))
خ is the problem that happened, and خ.رسالة describes it.
ارفع: raising a problem yourself¶
ارفع («raise») stops the work with a problem of your own. Here, a withdrawal (اسحب) that
asks for more than the balance (الرصيد):
دالة اسحب(الرصيد: عشري، المبلغ: عشري): عشري {
إذا المبلغ > الرصيد {
ارفع مشكلة("الرصيد لا يكفي: معك {الرصيد} وتريد {المبلغ}")
}
أرجع الرصيد - المبلغ
}
اطبع(اسحب(100، 30))
حاول {
اطبع(اسحب(100، 250))
} التقط خ {
اطبع("لم تتم العملية:"، خ.رسالة)
}
Find mistakes before running¶
dad check checks a program without running it, and shows everything it finds. It is what
VS Code does as you type:
خطأ [السطر 5، العمود 7]: القيمة من النوع «نقطة» لا تحتوي «ع»؛ هل تقصد «س»؟
خطأ [السطر 6، العمود 6]: «نقطه» غير معرّف؛ هل تقصد «نقطة»؟
Notice «هل تقصد …؟» («did you mean …?»): Ḍād suggests the name closest to what you wrote, س
for the field and نقطة for the type.
Exercise¶
Write a function قسمة_آمنة(أ: عشري، ب: عشري): عشري؟ («safe division») that returns عدم if
ب is zero, and the result of the division otherwise. Then print قسمة_آمنة(10، 4) and
قسمة_آمنة(1، 0) ؟؟ "غير معرّفة" («undefined»).
Solution
Next: Your first page