0Pricing
Zig Academy · Ders

Hataları Sarma ve Yeniden Fırlatma

Hatalar üst katmanlara ilerlerken bağlam ekleyin.

Hataları Sarma ve Yeniden Fırlatma, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 4. 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.

Errors Travel Upward

When a function cannot handle a failure itself, it passes the error to its caller. Along the way you can adjust or re-throw it.

Re-throw with try

The simplest re-throw is try. It forwards the original error unchanged to your caller, so the same value keeps bubbling up.

const data = try readFile(path);

Catch Then Return

To translate an error, catch it and return a different one. This swaps a low-level error for one that fits your own API.

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

Map Errors with switch

Combine catch with a switch to map each underlying error to a clearer one, while passing the rest through unchanged.

parse(s) catch |e| switch (e) {
    error.Empty => return error.NoInput,
    else => return e,
};

Zig Has No Stack Traces by Value

An error in Zig is just a tag, not an object holding context. To add detail you re-throw a more descriptive error of your own.

Attach Context Separately

Since the error tag carries no message, log the details where the failure happens, then return the higher-level error to the caller.

open(path) catch |e| {
    std.log.err("open {s}: {}", .{ path, e });
    return error.LoadFailed;
};

Widen the Error Set

If you return a new error, your function's error set must include it. An inferred !T set grows to cover everything you return.

fn load() !Config {
    return open(".cfg") catch error.LoadFailed;
}

Preserve or Replace

Decide per layer: forward the exact error with try, or replace it with one that makes more sense to your caller. Both are valid.

Keep Boundaries Clean

At a module boundary, map internal errors to a small public set. Callers then depend on stable names, not your internals.

Pair with errdefer

When you re-throw, any errdefer in scope still fires first. So you can rollback resources and translate the error in one path.

Meaningful Failures

Wrapping errors turns a raw cause into a message that fits your domain. The caller sees a clear error instead of an internal detail.

Quick Check

You want to replace a low-level error.NotFound with your own error.ConfigMissing for the caller. Which works?

Recap

You re-threw errors with try, mapped them via catch and switch, logged context, and widened error sets to keep failures meaningful. ✅

Sıkça Sorulan Sorular

“Hataları Sarma ve Yeniden Fırlatma” dersi ücretsiz mi?

Evet — “Hataları Sarma ve Yeniden Fırlatma” 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.

“Hataları Sarma ve Yeniden Fırlatma” dersinde ne öğreneceğim?

Hatalar üst katmanlara ilerlerken bağlam ekleyin. 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 4. dersidir.

“Hataları Sarma ve Yeniden Fırlatma” 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

  1. catch ile Kurtarma
  2. Belirli Hatalara Göre switch Kullanma
  3. Başarısızlık Temizliği İçin errdefer
  4. Hataları Sarma ve Yeniden Fırlatma
← Zig Academy Sayfasına Dön