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.
日本語
「値なし」のための送別値(Sentinel)
- 送別値とは「そこに何もない」という意味の特別な値である:インデックスには
-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. · 実行ボタンをクリックして出力を確認してください。