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
- ภาษาบางภาษา throw exceptions เมื่อเกิดปัญหา C ไม่มี exceptions
- แทนที่, C function รายงานปัญหาผ่าน return value — status code
- ผู้เรียกตรวจสอบโค้ดนี้หลังทุก call และตัดสินใจว่าจะทำอะไร
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).
คืนค่า status code
- กฎทั่วไป: คืน
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.
รายงานคำตอบผ่าน out-pointer
- ถ้า return value เป็น status code, ผลลัพธ์จริงไปที่ไหน? ผ่าน out-pointer
- ผู้เรียกส่งที่อยู่ของตัวแปร; ฟังก์ชันเขียนคำตอบด้วย
* - เมื่อเกิดข้อผิดพลาด ฟังก์ชันจะส่งค่า
-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 สำหรับ "ไม่มีค่า"
- Sentinel คือค่าพิเศษที่หมายถึง "ไม่มีอะไรอยู่ที่นี่":
-1สำหรับดัชนี,NULLสำหรับพอยเตอร์ - ฟังก์ชันใช้ sentinel เพื่อให้ผู้เรียกสามารถแยกความแตกต่างระหว่างความสำเร็จและความล้มเหลวด้วยการตรวจสอบอย่างง่าย
- ตรวจสอบ sentinel เสมอก่อนที่คุณจะเชื่อถือผลลัพธ์
#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กลับเมื่อล้มเหลว แล้วเขียนคำตอบจริงผ่าน out-pointer - เมื่อล้มเหลว อย่าสัมผัส out-pointer อย่า เขียน
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 ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. อย่าเขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่