Skip to content
العربية

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.

A function returning !عدد64Success: the value · failure: خطأ.اسمن = ف() والا 0Handled here; the program goes onن = جرّب ف()Goes up to the callerTop level, unhandled«Unhandled error», code 3
A handled error has two paths: it is handled on the spot with «والا», or it goes up with «جرّب». One that reaches the top level unhandled ends the program with code 3.
  • !نوع 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): ف() والا 0 is 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:
خاتمه and خاتمه_عند_خطأ
اخطاء:
ملف_تالف
داله حمّل(صالح منطقي) !عدد64:
اطبع("فتح")
خاتمه اطبع("إغلاق")
خاتمه_عند_خطأ اطبع("تراجع عن نصف العمل")
اذا !صالح:
ارجع خطأ.ملف_تالف
ارجع 42
اطبع(حمّل(صواب) والا -1)
اطبع("---")
اطبع(حمّل(خطاء) والا -1)
Output recorded from the compilermeem اخطاء_عند_خطأ.م · meem 0.17.3+434e13f
فتحإغلاق42---فتحتراجع عن نصف العملإغلاق-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:

A !عدد64 result left unhandled
داله نصف(ن نص) !عدد64:
ارجع جرّب حلل(ن, عدد64) / 2
اطبع(نصف("8"))
Output recorded from the compilermeem 15_غير_معالج.م · meem 0.17.3+434e13f
​[خطأ دلالي E0399] سطر 4:6 - 'نصف(...)' من النوع '!عدد64' (قد يفشل) ولا يُستعمل كأنه 'عدد64'   4 | اطبع(نصف("8"))     |      ^~~  تلميح: عالج الخطأ في موضعه:  جرّب نصف(...)  (يمرّر الخطأ إلى مستدعي الدالة)   أو   نصف(...) والا قيمة_بديلة   أو   نصف(...) والا خ:  ثم كتلة تعالج «خ»

Exit code: 2

In the error catalog: E0399

And the reference handbook’s full example:

Example
اخطاء:
رصيد_غير_كاف
داله اسحب(رصيد عدد64, مبلغ عدد64) !عدد64:
اذا مبلغ > رصيد:
ارجع خطأ.رصيد_غير_كاف
ارجع رصيد - مبلغ
داله سحبان(رصيد عدد64, مبلغ عدد64) !عدد64:
خاتمه_عند_خطأ اطبع("أُلغيت العملية")
بعد_الاول = جرّب اسحب(رصيد, مبلغ)
ارجع جرّب اسحب(بعد_الاول, مبلغ)
اطبع(سحبان(100, 30) والا -1)
باقي = سحبان(50, 30) والا خ:
حالات خ:
حاله خطأ.رصيد_غير_كاف:
اطبع("الرصيد لا يكفي")
0
حاله _:
اطبع("خطأ آخر: ", خ)
-1
اطبع(باقي)
ن = حلل("٤٢", عدد64) والا 0
اطبع(ن + 1, " ", حلل("س", عدد64) والا -1)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
40أُلغيت العمليةالرصيد لا يكفي043 -1

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:

خاتمه does not run on a runtime error
داله احسب(ا عدد64, ب عدد64) عدد64:
خاتمه اطبع("تنظيف")
ارجع ا / ب
اطبع(احسب(10, 2))
اطبع(احسب(10, 0))
Output recorded from the compilermeem اخطاء_تشغيل.م · meem 0.17.3+434e13f
تنظيف5
[خطأ تشغيل E0403] سطر 3:12 - القسمة على صفر      ارجع ا / ب

Exit code: 3

In the error catalog: E0403

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)
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.

Lesson 16: error handling · Error handling in the reference · Error catalog · Migrating to error handling