Error handling with return codes · معالجة الأخطاء مع رموز الإرجاع
C has no try/except
- Some languages throw exceptions when something goes wrong. C does not have exceptions.
- Instead, a C function reports trouble through its return value — a status code.
- The caller checks that code after every call and decides what to do.
C لا تحتوي على try/except
- بعض اللغات تُلقِ استثناءات عند حدوث خطأ. C لا تحتوي على استثناءات.
- بدلاً من ذلك، تُبلغ دالة C عن المشكلة عبر قيمة الإرجاع — رمز حالة.
- المُدْعِي يتحقق من هذا الرمز بعد كل استدعاء ويقرر ما يجب فعله.
Return a status code
- A common rule: return
0for success and a negative number like-1for failure. - So
int do_thing(...)returns0when it worked,-1when it could not. - The real "answer" is then handed back a different way (see the next step).
إرجاع رمز حالة
- قاعدة شائعة: إرجاع
0لـ النجاح ورقم سالب مثل-1لـ الفشل. - لذا فإن
int do_thing(...)تُرجع0عند النجاح، و-1عند الفشل. - الـ "الإجابة" الحقيقية تُعاد بطريق مختلف (راجع الخطوة التالية).
Reporting the answer through an out-pointer
- If the return value is a status code, where does the real result go? Through an out-pointer.
- The caller passes the address of a variable; the function writes the answer with
*. - On failure, the function returns
-1and simply leaves that variable alone.
الإبلاغ عن الإجابة عبر مؤشر إخراج
- إذا كانت قيمة الإرجاع رمز حالة، أين تذهب النتيجة الفعلية؟ عبر مؤشر إخراج.
- المُدْعِي يمر عنوان متغير؛ تكتب الدالة الإجابة باستخدام
*. - عند الفشل، تُرجع الدالة
-1وتترك المتغير دون تغيير.
Sentinels for "no value"
- A sentinel is a special value that means "nothing here":
-1for an index,NULLfor a pointer. - Functions use sentinels so the caller can tell success from failure with a simple check.
- Always check for the sentinel before you trust the result.
رموز "لا قيمة"
- القيمة المحورية (Sentinel) هي قيمة خاصة تعني "لا شيء هنا":
-1للفهرس، وNULLللمؤشر. - تستخدم الدوال الرموز حتى يتمكن المُدْعِي من التمييز بين النجاح والفشل بفحص بسيط.
- تحقق دائمًا من وجود الرمز قبل الوثوق بالنتيجة.
#include <stdio.h>
int safe_div(int a, int b, int *result) {
if (b == 0) return -1; // failure: cannot divide by zero
*result = a / b; // success: hand back the answer
return 0;
}
int main(void) {
int r;
if (safe_div(10, 2, &r) == 0) {
printf("%d\n", r); // 5
}
return 0;
}
Common mistakes
- Return a non-zero code from
mainto signal an error. - Check the return value of any function that can fail.
أخطاء شائعة
- أدر كودًا غير صفري من
mainللإشارة إلى خطأ. - تحقق من قيمة الإرجاع لأي دالة قد تفشل.
Now you try
- Return
0on success and-1on failure, and write the real answer through the out-pointer. - On failure, do not touch the out-pointer. Do not write a
main— the checker provides one.
الآن جرب بنفسك
- أدر
0في حالة النجاح و-1في حالة الفشل، واكتب الإجابة الحقيقية عبر المؤشر الخارجي. - عند الفشل، لا تلمس المؤشر الخارجي. لا تكتب
main— المحقق يوفر واحدًا.
Complete int safe_div(int a, int b, int *result): if b is 0, return -1 and leave *result alone. Otherwise store a / b in *result and return 0. Do not write a main. · أكمل int safe_div(int a, int b, int *result): إذا كان b هو 0، أرجع -1 واترك *result دون تغيير. وإلا خزن a / b في *result وأرجع 0. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int parse_int(const char *s, int *out): if s is a valid integer (digits, with an optional leading -), store its value in *out and return 0; otherwise return -1. Do not write a main. · أكمل int parse_int(const char *s, int *out): إذا كان s رقماً صحيحاً صالحاً (أرقام، مع إشارة - اختيارية في البداية)، خزن قيمته في *out وأرجع 0؛ وإلا أرجع -1. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int withdraw(int balance, int amount, int *new_balance): if amount is more than balance, return -1. Otherwise set *new_balance to balance - amount and return 0. Do not write a main. · أكمل int withdraw(int balance, int amount, int *new_balance): إذا كان amount أكبر من balance، أعد -1. وإلا تعيين *new_balance إلى balance - amount وأعد 0. لا تكتب ⟩ main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.