--- id: c-error-handling title: "C Error Handling" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["perror strerror errno", "exit EXIT_FAILURE", "C μ—λŸ¬ 처리"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "error-handling", "errno"] raw_sources: ["https://www.w3schools.com/c/c_error_handling.php"] applied_in: [] github_commit: "" --- # [[C Error Handling]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) C has NO try/catch mechanism at all β€” the source states this directly β€” meaning every error-handling technique in this chapter (checking for `NULL` returns, `perror()`, `strerror(errno)`, comparing `errno` to constants like `ENOENT`) is a WORKAROUND built from ordinary control flow (if-statements and return values), not a language feature, which is why disciplined manual NULL-checking after every risky operation is non-negotiable in C in a way it isn't in exception-based languages. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **No built-in exceptions** β€” C lacks try/catch; error handling relies on return values, global error codes (`errno`), and helper functions. [S1] - **NULL-return checking** β€” many functions (like `fopen()`) return `NULL` on failure; checking with `if (ptr == NULL)` is the primary detection mechanism. [S1] - **`perror(message)`** β€” prints a custom message plus a system-provided description of the last error. [S1] - **`errno` + `strerror(errno)`** β€” `errno` (from ``) holds the last failed operation's error code; `strerror()` converts it to a readable string. [S1] - **Named error constants** β€” `ENOENT` (no such file/directory), `EACCES` (permission denied), `ENOMEM` (out of memory), `EINVAL` (invalid argument) β€” allow branching on the SPECIFIC failure reason. [S1] - **`exit(code)`** β€” immediately terminates the program with a status code; `0`/`EXIT_SUCCESS` means success, non-zero/`EXIT_FAILURE` signals an error to the OS. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic NULL check: `FILE *fptr = fopen("nothing.txt", "r"); if (fptr == NULL) { printf("Error opening file.\n"); return 1; }`. [S1] - Detailed error via `perror()`: `perror("Error opening file"); // "Error opening file: No such file or directory"`. [S1] - Branching on a specific error code: `if (errno == ENOENT) { printf("The file was not found.\n"); } else { printf("Some other file error occurred.\n"); }`. [S1] - Exiting cleanly with readable status constants: `if (f == NULL) { perror("Could not open nothing.txt"); exit(EXIT_FAILURE); } return EXIT_SUCCESS;`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **Cμ—λŠ” try/catchκ°€ μ—†μŒ**: λ‹€λ₯Έ 언어와 달리 μ˜ˆμ™Έ 처리 λ©”μ»€λ‹ˆμ¦˜μ΄ μ•„μ˜ˆ μ—†κ³ , λ°˜ν™˜κ°’Β·errnoΒ·perror()/strerror() 같은 도ꡬλ₯Ό μ‘°ν•©ν•΄ 직접 μ—λŸ¬λ₯Ό κ°μ§€ν•˜κ³  λŒ€μ‘ν•΄μ•Ό ν•œλ‹€λŠ” 점이 λͺ…μ‹œμ μœΌλ‘œ 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 파일(nothing.txt)을 μ—΄λ €λŠ” μ‹œλ„κ°€ μ‹€νŒ¨ν•˜λŠ” 상황을 NULL 체크, perror(), strerror(errno), errno 비ꡐ, exit() λ“± λ‹€μ„― κ°€μ§€ λ°©μ‹μœΌλ‘œ 반볡 μ œμ‹œν•˜λ©° 각 κΈ°λ²•μ˜ μ‹€μ „ ν™œμš©μ„ 직접 λ³΄μ—¬μ€Œ. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Detecting a specific error condition via errno and reporting it with a custom message (C): ```c #include FILE *f = fopen("nothing.txt", "r"); if (f == NULL) { if (errno == ENOENT) { printf("The file was not found.\n"); } else { printf("Some other file error occurred.\n"); } return 1; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C Errors]], [[C Null]], [[C Input Validation]] - **μ°Έμ‘° λ§₯락:** μ—λŸ¬ 처리 β€” μž…λ ₯ 검증(Input Validation) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Error Handling β€” https://www.w3schools.com/c/c_error_handling.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Error Handling" page (Astra wiki-curation, P-Reinforce v3.1 format).