0Pricing
Mojo Academy · Lección

Capturar con try/except

Recupérese de un fallo correctamente.

Capturar con try/except es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «Capturar con try/except» es gratis?

Sí — el texto completo de «Capturar con try/except» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Capturar con try/except»?

Recupérese de un fallo correctamente. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Capturar con try/except»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Lanzar un error
  2. Capturar con try/except
  3. La anotación raises
  4. Limpiar con finally
← Volver a Mojo Academy