التقاط الأخطاء باستخدام try/except
استعد من الفشل بسلاسة
التقاط الأخطاء باستخدام try/except درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Catching the Fall
Raising signals a problem, but someone must catch it. Mojo uses a try block to run risky code and recover if it fails. 🪤
The try Block
Code that might fail goes inside try. If it raises, Mojo jumps straight to the matching recovery code below.
try:
risky_step()The except Block
The except block holds your recovery plan. It only runs when the try block actually raised an error.
try:
risky_step()
except:
print("recovered")Reading the Error
You can name the caught error to read its message. Here e holds the details of what went wrong.
try:
risky_step()
except e:
print("failed:", e)Normal Path First
If nothing raises, the try block finishes and the except block is skipped entirely. The happy path stays clean.
Recovering Gracefully
Catching lets you show a friendly message or use a fallback value instead of letting the whole program crash. ✨
Keep try Blocks Small
Wrap only the risky line in try, not your whole function. Small blocks make it clear what could fail.
Catching Calls That Raise
Calling a function that may raise requires a try. Here we safely attempt a withdrawal and handle a rejection.
try:
withdraw(-5)
except e:
print("denied:", e)Don't Swallow Silently
An empty except that does nothing hides bugs. At minimum log the error so problems stay visible.
Catch Where You Can Help
Only catch an error where you can actually do something useful about it. Otherwise let it propagate upward.
Try Plus Except as a Pair
Together try and except turn a possible failure into a controlled outcome your program can continue past.
Quick Check
Recall when each block of a try/except actually runs.
Recap
You learned to wrap risky code in try, recover in except, read the error with a name, and keep blocks small and meaningful. 🎯
الأسئلة الشائعة
هل درس «التقاط الأخطاء باستخدام try/except» مجاني؟
نعم — نص درس «التقاط الأخطاء باستخدام try/except» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «التقاط الأخطاء باستخدام try/except»؟
استعد من الفشل بسلاسة تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التقاط الأخطاء باستخدام try/except»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- رفع خطأ
- التقاط الأخطاء باستخدام try/except
- التعليمة التوضيحية raises
- التنظيف باستخدام finally