0Pricing
Mojo Academy · Leçon

Intercepter avec try/except

Récupérez élégamment après un échec.

Intercepter avec try/except est une leçon Mojo Academy gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Mojo Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Mojo Academy comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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. 🎯

Questions Fréquemment Posées

La leçon « Intercepter avec try/except » est-elle gratuite ?

Oui — le texte complet de « Intercepter avec try/except » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Mojo Academy, passe à CoddyKit PRO. Le cours Mojo Academy comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Intercepter avec try/except » ?

Récupérez élégamment après un échec. Tu pratiques Mojo Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Mojo Academy ?

Aucune expérience préalable n'est requise. Mojo Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.

Combien de temps prend la leçon « Intercepter avec try/except » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Mojo Academy ?

Oui. Chaque leçon Mojo Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Lever une erreur
  2. Intercepter avec try/except
  3. L’annotation raises
  4. Nettoyer avec finally
← Retour à Mojo Academy