لف الأخطاء وإعادة طرحها
أضيفوا السياق أثناء صعود الأخطاء
لف الأخطاء وإعادة طرحها درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. ✅
الأسئلة الشائعة
هل درس «لف الأخطاء وإعادة طرحها» مجاني؟
نعم — نص درس «لف الأخطاء وإعادة طرحها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.
ماذا ستتعلم في «لف الأخطاء وإعادة طرحها»؟
أضيفوا السياق أثناء صعود الأخطاء تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟
لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «لف الأخطاء وإعادة طرحها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟
نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التعافي باستخدام catch
- التبديل على أخطاء محددة
- errdefer لتنظيف ما بعد الفشل
- لف الأخطاء وإعادة طرحها