Dealing with errors
In short: Meem has two kinds of errors. A handled error is a value returned by a function that can fail (!نوع), and you must handle it at compile time. A runtime error (an index outside a list, division by zero, overflow) is a fault in the program: it cannot be handled, and it ends the program with code 3.
The four words
Section titled “The four words”!نوعin a function’s signature: it can fail. When it fails it returnsخطأ.اسم(error.name), and your error names live in anاخطاء:(errors) block.والا(else):ف() والا 0is a fallback value, andف() والا خ:followed by a block givesخas the error’s name (withحالات خto tell errors apart); the last line of the block is the fallback value.جرّب(try): inside a!نوعfunction, it passes the failure to the caller. At the top level, it ends the program with code 3 if it fails.خاتمه_عند_خطأ(defer on error): clean-up that runs only if the function exits with an error, and before theخاتمهdeclared before it:
اخطاء: ملف_تالف
داله حمّل(صالح منطقي) !عدد64: اطبع("فتح") خاتمه اطبع("إغلاق") خاتمه_عند_خطأ اطبع("تراجع عن نصف العمل") اذا !صالح: ارجع خطأ.ملف_تالف ارجع 42
اطبع(حمّل(صواب) والا -1)اطبع("---")اطبع(حمّل(خطاء) والا -1)Forgetting to handle a failure is a compile error, so no program reaches run time with a failure you have not decided what to do with:
داله نصف(ن نص) !عدد64: ارجع جرّب حلل(ن, عدد64) / 2
اطبع(نصف("8"))Exit code: 2
In the error catalog: E0399
And the reference handbook’s full example:
اخطاء: رصيد_غير_كاف
داله اسحب(رصيد عدد64, مبلغ عدد64) !عدد64: اذا مبلغ > رصيد: ارجع خطأ.رصيد_غير_كاف ارجع رصيد - مبلغ
داله سحبان(رصيد عدد64, مبلغ عدد64) !عدد64: خاتمه_عند_خطأ اطبع("أُلغيت العملية") بعد_الاول = جرّب اسحب(رصيد, مبلغ) ارجع جرّب اسحب(بعد_الاول, مبلغ)
اطبع(سحبان(100, 30) والا -1)باقي = سحبان(50, 30) والا خ: حالات خ: حاله خطأ.رصيد_غير_كاف: اطبع("الرصيد لا يكفي") 0 حاله _: اطبع("خطأ آخر: ", خ) -1اطبع(باقي)ن = حلل("٤٢", عدد64) والا 0اطبع(ن + 1, " ", حلل("س", عدد64) والا -1)A runtime error is not a handled error
Section titled “A runtime error is not a handled error”Division by zero, an index outside a list, and overflowing the type’s width are programming mistakes: والا does not catch them, and they end the program with code 3, an ID and a line. And خاتمه does not run when they happen, so a temporary file that was going to be deleted stays on disk:
داله احسب(ا عدد64, ب عدد64) عدد64: خاتمه اطبع("تنظيف") ارجع ا / ب
اطبع(احسب(10, 2))اطبع(احسب(10, 0))Exit code: 3
In the error catalog: E0403
What changed since 0.10.0
Section titled “What changed since 0.10.0”This page listed seven limits in 0.10.0, and most of them are solved:
| In 0.10.0 | Now |
|---|---|
| An error is a name without data | An error has context (since 0.11.0): ارجع خطأ.اسم مع قيم attaches a message, and the library’s errors carry the path and the system error number, or the bad text |
| One error set | An error set in the signature is optional: !{غير_موجود, ممنوع} نص, and a حالات that misses a case is error E0449, which names it |
| No chain and no cause | بسبب خ (because of) keeps the earlier error, and سبب(خ) reads it (since 0.11.0) |
| No trace | «نشأ في» (originated in) with the place of the first ارجع that returned it, in every mode, and «ثم» (then) for each جرّب it passed through in a direct run and build --debug (since 0.11.0) |
خاتمه does not accept والا |
خاتمه اغلق(م) والا خ: and خاتمه جرّب are accepted (since 0.11.0) |
A function pointer cannot return !نوع |
داله(نص) !عدد64 is a function pointer type (since 0.14.0) |
errno from C is not available |
نظام.خطأ_النظام() and رقم_النظام(خ) (since 0.14.0) |
Limits in 0.17.1
Section titled “Limits in 0.17.1”| Limit | What it means |
|---|---|
| The context is a message, not typed data | What is attached to an error is text and a cause, not a value of a type you choose |
| The context store is fixed | 64 errors: an error stored and then followed by 64 newer ones keeps its name, and its context becomes «لم يعد متاحاً» (no longer available); copy رسالة(خ, م) if you want it kept |
| A function pointer’s error set | It is accepted in the type, and its containment is not checked: a call through the pointer has an open set |
| A runtime error is not a value | It ends the program with code 3, and خاتمه does not run (above) |
The strength: handling is enforced at compile time with three short words, خاتمه_عند_خطأ runs in the right order across functions, the successful path costs nothing, and the error context sits in a fixed store with no allocation on the failure path. For ease and safety it is close to Zig; for expressiveness it is weaker than Rust, because what is attached to an error is text, not typed data.
See also
Section titled “See also”Lesson 16: error handling · Error handling in the reference · Error catalog · Migrating to error handling