Skip to content

Error handling with return codes · ⁨用返回码处理错误⁩

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 的函数通过它的返回值 —— 一个状态码 —— 来报告问题。
  • 调用者在每次调用后检查这个状态码,再决定怎么做。

函数成功返回 0、失败返回 -1;调用者检查这个返回码

Handout

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩