Files
2nd/10_Wiki/Dev/Topic_C/C_Error_Handling.md
T
Antigravity Agent 1cfd3bbb56 docs(10_Wiki): 위키 구조 정리 — 언어 튜토리얼 카테고리 폴더 제거 + 신규 자산 동기화
Topic_CSS/Topic_HTML/Topic_JavaScript/Topic_Prompt/Topic_Comfyui 등 기존 카테고리 폴더를 정리하고,
Topic_Graphic/Dev 등 신규 산출물과 Topics 내부 세션/메모리 기록을 동기화.
2026-07-05 00:10:59 +09:00

4.4 KiB

id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
id title category status verification_status canonical_id aliases duplicate_of source_trust_level confidence_score created_at updated_at review_reason merge_history tags raw_sources applied_in github_commit
c-error-handling C Error Handling Programming_Language draft conceptual
perror strerror errno
exit EXIT_FAILURE
C 에러 처리
B 0.87 2026-07-04 2026-07-04
c
programming-language
w3schools
error-handling
errno
https://www.w3schools.com/c/c_error_handling.php

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 <errno.h>) holds the last failed operation's error code; strerror() converts it to a readable string. [S1]
  • Named error constantsENOENT (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):

#include <errno.h>
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)

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-07-04: Initial draft synthesized from the W3Schools "C Error Handling" page (Astra wiki-curation, P-Reinforce v3.1 format).