Meneruskan Kegagalan dengan try
Teruskan kesalahan ke atas dengan satu kata kunci.
Meneruskan Kegagalan dengan try adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Zig Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Zig Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
Handling Gets Repetitive
Writing an if-else around every fallible call to forward its error gets noisy fast. Zig offers a shortcut: the try keyword.
try Unwraps or Returns
Put try before a fallible call. On success it hands you the value; on error it returns that error from your current function.
const n = try parse(input);It Is Pure Sugar
That one line is exactly the same as catching the error and returning it yourself. try just writes the boilerplate for you.
const n = parse(input) catch |e| return e;Your Function Must Be Fallible
Because try can return an error, the enclosing function needs an error union return type. Otherwise the compiler rejects it.
fn run() !void {
const n = try parse(input);
}Chain Many tries
Stack several try calls in a row. Each one stops the function early on failure, so the happy path reads top to bottom.
const f = try open(path);
const data = try read(f);try in the Middle of Expressions
You can place try right inside a larger expression. The unwrapped value flows straight into the surrounding call or math.
const total = base + try parse(input);try with void Calls
Even when a call returns !void, use try to forward any failure. You get no value back, only the early return on error.
try file.writeAll(data);Errors Climb the Stack
With try in every layer, a failure deep down propagates all the way up until some caller finally decides to handle it.
Readable Happy Path
The big win is clarity: error forwarding fades into a single keyword, so the main logic stays front and center.
try Does Not Handle
Remember that try only forwards an error; it never recovers from one. To react locally you reach for catch instead.
When to Forward
Use try when the right move is to let the caller deal with failure. Most intermediate functions simply pass errors upward.
Quick Check
You write try doThing() inside a function. What happens if doThing returns an error?
Recap
You learned that try unwraps on success and returns the error otherwise. It is shorthand for catch and forward, keeping code clean. ✅
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Meneruskan Kegagalan dengan try” gratis?
Ya — teks lengkap “Meneruskan Kegagalan dengan try” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Zig Academy, upgrade ke CoddyKit PRO. Kursus Zig Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Meneruskan Kegagalan dengan try”?
Teruskan kesalahan ke atas dengan satu kata kunci. Kamu berlatih Zig Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Zig Academy?
Tidak diperlukan pengalaman sebelumnya. Zig Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.
Berapa lama pelajaran “Meneruskan Kegagalan dengan try” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Zig Academy ini?
Ya. Setiap pelajaran Zig Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Kumpulan Kesalahan dan Union !T
- Mengembalikan Kesalahan dari Fungsi
- Meneruskan Kegagalan dengan try
- Kumpulan Kesalahan Terinferensi