0Pricing
Zig Academy · レッスン

catchで復旧する

エラーが発生したときの代替処理を用意します。

「catchで復旧する」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Errors Need a Plan

A function can return an error union like !u32. Before you use the result you must decide what happens on the failure path.

Meet catch

The catch operator turns an error union into a plain value by giving a fallback to use whenever the call returns an error.

const n = parse(text) catch 0;

The Fallback Replaces the Error

If the call succeeds you get the real value. If it errors you get the right side of catch instead, so n is always a usable number.

const n = parse(text) catch -1;

catch Is an Expression

Because catch yields a value, you can drop it inline anywhere a value is expected, like straight into a function argument.

print(parse(text) catch 0);

Capture the Error

Add a pipe capture to inspect what went wrong. The name inside the bars holds the specific error value that was returned.

const n = parse(text) catch |err| {
    log(err);
    return 0;
};

catch Can Run a Block

The right side of catch may be a whole block. Use it to log, compute a fallback, or break out of a loop before continuing.

const v = open(path) catch |e| blk: {
    break :blk fallback;
};

catch Can Return Early

You can return from inside catch to leave the function when recovery is impossible. The code after it never runs on error.

const f = open(path) catch return error.MissingFile;

catch unreachable

Writing catch unreachable asserts the call cannot fail. In a safe build a real error here panics instead of being ignored.

const n = parse("42") catch unreachable;

Use unreachable Carefully

Reach for catch unreachable only when an error is truly impossible. If you are unsure, supply a real fallback instead.

catch vs try

Use catch when you can handle the error here and now. Use try when you would rather pass the error up to the caller.

Recovery Is Explicit

Every error union forces a choice, so a failure can never be silently dropped. With catch you decide exactly how to recover.

Quick Check

You want to keep going with a safe default when a call fails. Which expression fits?

Recap

You used catch to recover from errors with a fallback, a block, an early return, or an assertion. Failure is always handled on purpose. ✅

よくある質問

「catchで復旧する」レッスンは無料ですか?

はい。「catchで復旧する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。

「catchで復旧する」で何を学びますか?

エラーが発生したときの代替処理を用意します。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Zig Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「catchで復旧する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このZig Academyレッスンでコードを書いて実行できますか?

はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. catchで復旧する
  2. 特定のエラーをswitchで処理する
  3. 失敗時の後始末にerrdeferを使う
  4. エラーをラップして再スローする
← Zig Academyに戻る