関数からエラーを返す
シグネチャで失敗を明示します。
「関数からエラーを返す」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Failure in the Signature
When a function can fail, say so in its return type. An error union like ParseError!u32 warns every caller up front.
fn parse(s: []const u8) ParseError!u32 {
// ...
}Return the Success Value
On the happy path you just return the payload as normal. Zig wraps that plain value into the success side of the union for you.
return 42;Return an Error Instead
To fail, return one of your errors in the same spot. The single return statement decides which side of the union you produce.
if (s.len == 0) return error.Empty;One Return, Two Outcomes
A function with an error union can return a value in one branch and an error in another. Both satisfy the declared type.
if (ok) return value;
return error.Failed;Void Plus Errors
A function that does work but yields no value can still fail. Use !void so it returns nothing on success or an error on failure.
fn save() !void {
// ...
}Errors Bubble at the Call Site
When you call a fallible function, you receive an error union too. You cannot ignore it; the compiler makes you handle it.
const n = parse(input);Capture Errors with if
An if can split the union: capture the value in the main branch and the error after else, naming each with a pipe.
if (parse(input)) |n| {
use(n);
} else |err| {
report(err);
}Name the Error Set Precisely
Listing the exact set before ! keeps your contract honest. Callers learn the full list of failures without reading the body.
fn open(p: []const u8) FileError!File {}Errors Cross Function Boundaries
An error returned deep inside your code travels outward only when each function in the chain declares it can fail too.
Make Failure Explicit
This is the heart of Zig error handling: failure is never hidden. The signature is a promise about what can go wrong.
Compiler Has Your Back
Forget to handle a returned error and the build fails. Zig refuses to let a possible failure silently slip through.
Quick Check
A function does work but returns no useful value, yet it might fail. What return type fits?
Recap
You declared fallible functions with !T, returned values or errors from one return, and saw the compiler force callers to handle them. ✅
よくある質問
「関数からエラーを返す」レッスンは無料ですか?
はい。「関数からエラーを返す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。
「関数からエラーを返す」で何を学びますか?
シグネチャで失敗を明示します。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Zig Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「関数からエラーを返す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このZig Academyレッスンでコードを書いて実行できますか?
はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- エラーセットと!Tユニオン
- 関数からエラーを返す
- tryで失敗を伝播させる
- 推論されるエラーセット