0Pricing
Zig Academy · درس

التعافي باستخدام catch

وفّروا بديلاً عند حدوث خطأ

التعافي باستخدام catch درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Errors Need a Plan

A function can return an error union like !u32. Before you use the result you must decide what happens on the failure path.

Meet catch

The catch operator turns an error union into a plain value by giving a fallback to use whenever the call returns an error.

const n = parse(text) catch 0;

The Fallback Replaces the Error

If the call succeeds you get the real value. If it errors you get the right side of catch instead, so n is always a usable number.

const n = parse(text) catch -1;

catch Is an Expression

Because catch yields a value, you can drop it inline anywhere a value is expected, like straight into a function argument.

print(parse(text) catch 0);

Capture the Error

Add a pipe capture to inspect what went wrong. The name inside the bars holds the specific error value that was returned.

const n = parse(text) catch |err| {
    log(err);
    return 0;
};

catch Can Run a Block

The right side of catch may be a whole block. Use it to log, compute a fallback, or break out of a loop before continuing.

const v = open(path) catch |e| blk: {
    break :blk fallback;
};

catch Can Return Early

You can return from inside catch to leave the function when recovery is impossible. The code after it never runs on error.

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

catch unreachable

Writing catch unreachable asserts the call cannot fail. In a safe build a real error here panics instead of being ignored.

const n = parse("42") catch unreachable;

Use unreachable Carefully

Reach for catch unreachable only when an error is truly impossible. If you are unsure, supply a real fallback instead.

catch vs try

Use catch when you can handle the error here and now. Use try when you would rather pass the error up to the caller.

Recovery Is Explicit

Every error union forces a choice, so a failure can never be silently dropped. With catch you decide exactly how to recover.

Quick Check

You want to keep going with a safe default when a call fails. Which expression fits?

Recap

You used catch to recover from errors with a fallback, a block, an early return, or an assertion. Failure is always handled on purpose. ✅

الأسئلة الشائعة

هل درس «التعافي باستخدام catch» مجاني؟

نعم — نص درس «التعافي باستخدام catch» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.

ماذا ستتعلم في «التعافي باستخدام catch»؟

وفّروا بديلاً عند حدوث خطأ تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟

لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «التعافي باستخدام catch»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟

نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التعافي باستخدام catch
  2. التبديل على أخطاء محددة
  3. ‏errdefer لتنظيف ما بعد الفشل
  4. لف الأخطاء وإعادة طرحها
← العودة إلى Zig Academy