İşlevlerden Hata Döndürme
Başarısızlığı imzalarda açıkça belirtin.
İşlevlerden Hata Döndürme, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Zig Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Zig Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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. ✅
Sıkça Sorulan Sorular
“İşlevlerden Hata Döndürme” dersi ücretsiz mi?
Evet — “İşlevlerden Hata Döndürme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Zig Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Zig Academy kursu toplamda 4 dersten oluşur.
“İşlevlerden Hata Döndürme” dersinde ne öğreneceğim?
Başarısızlığı imzalarda açıkça belirtin. Zig Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Zig Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Zig Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“İşlevlerden Hata Döndürme” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Zig Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Zig Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Hata Kümeleri ve !T Birleşimi
- İşlevlerden Hata Döndürme
- try ile Başarısızlıkları İletme
- Çıkarılan Hata Kümeleri