Kumpulan Kesalahan dan Union !T
Perlakukan kesalahan sebagai nilai kelas utama.
Kumpulan Kesalahan dan Union !T adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 1 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.
Errors Are Values
In Zig there are no exceptions. An error is just an ordinary value your function can return, so failure is visible in the code.
Declare an Error Set
An error set is a named group of possible errors. You list the variants much like an enum of things that can go wrong.
const FileError = error{
NotFound,
PermissionDenied,
};Refer to One Error
Each member of an error set is reached with a dot. The value FileError.NotFound is a single concrete error you can return.
return FileError.NotFound;Anonymous Error Sets
You do not always need a name. Writing error{Empty} inline creates a tiny one-off error set right where it is used.
return error.Empty;The Bang Means Maybe-Error
The ! in a type builds an error union. The type before it is the success value; an error may appear in its place instead.
FileError!u32Read !T Out Loud
You can read !u32 as either a u32 or an error. The single value carries both outcomes, so callers must pick one.
Two Sides of the Union
An error union has exactly two shapes at runtime: a valid payload of type T, or one of the errors from the set. Never both.
Set Before the Bang
Put the error set on the left of ! to say exactly which errors are allowed. This documents failure right in the type.
fn parse(s: []const u8) FileError!u32 {
// ...
}Merge Error Sets
The || operator on two error sets combines them into one. This lets a function admit errors from several sources at once.
const AllError = FileError || NetError;anyerror Is the Superset
The built-in anyerror contains every possible error. It is convenient but loses the precision of a named set, so use it sparingly.
fn risky() anyerror!void {}Why Sets Help
Because errors live in the type, the compiler knows every failure a function can produce. Nothing slips past you unhandled.
Quick Check
You see the type FileError!u32 on a function. What does it describe?
Recap
You met error sets, the !T union, set merging with ||, and anyerror. Errors are plain values the type system tracks for you. ✅
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Kumpulan Kesalahan dan Union !T” gratis?
Ya — teks lengkap “Kumpulan Kesalahan dan Union !T” 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 “Kumpulan Kesalahan dan Union !T”?
Perlakukan kesalahan sebagai nilai kelas utama. 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 1 dari 4.
Berapa lama pelajaran “Kumpulan Kesalahan dan Union !T” 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