Error handling with return codes
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
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 מדווחת על בעיה דרך ערך ההחזרה שלה — קוד סטטוס.
- הקורא בודק את הקוד הזה לאחר כל קריאה ומחליט מה לעשות.
English
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כאשר לא הצליחה. - התוצאה האמיתית "תשובה" מוחזרת אז בדרכים אחרות (ראה השלב הבא).
English
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.
עברית
דיווח על התוצאה דרך out-pointer
- אם ערך ההחזרה הוא קוד סטטוס, לאן הולכת התוצאה האמיתית? דרך out-pointer.
- הקורא מעביר את הכתובת של משתנה; הפונקציה כותבת את התשובה עם
*. - בכשל, הפונקציה מחזירה
-1והשאירה פשוט את המשתנה הזה ללא שינוי.
English
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.
עברית
שומרים עבור "אין ערך"
- שומר הוא ערך מיוחד שמסמן "אין כאן דבר":
-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;
}
English
Common mistakes
- Return a non-zero code from
mainto signal an error. - Check the return value of any function that can fail.
עברית
טעויות נפוצות
- החזר קוד לא-אפסי מ
mainכדי לסמן שגיאה. - בדוק את ערך ההחזרה של כל פונקציה שיכולה לכשל.
English
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.
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.
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.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.