GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Hello-Python/Hello-Python/Basic/CLASE_12.md at main · mrtngrsbch/Hello-Python

Expand file tree

/

CLASE_12.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
86 lines (59 loc) · 2.26 KB
/

CLASE_12.md

Copy path

File metadata and controls

86 lines (59 loc) · 2.26 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

📍 Inicio > Fundamentos > Clase 12

Clase 12 — Excepciones (hacer tu código robusto)

Apertura narrativa

Los errores ocurren. El manejo de excepciones permite anticiparlos, capturarlos y decidir cómo reaccionar sin que tu programa se derrumbe.

Por qué te importa

  • Evitas caídas inesperadas y mejoras la experiencia de usuario.
  • Diferencias entre errores recuperables y críticos.
  • Depuras mejor al capturar información del error.

Demostración guiada

# try/except/else/finally
try:
    x = int("42")
except ValueError as e:
    print("Conversión inválida", e)
else:
    print("Todo salió bien:", x)
finally:
    print("Se ejecuta siempre")

# Capturar por tipo específico
try:
    with open("no_existe.txt") as f:
        datos = f.read()
except FileNotFoundError:
    print("Archivo no encontrado")

# Levantar (raise) con mensaje

def dividir(a, b):
    if b == 0:
        raise ZeroDivisionError("b no puede ser 0")
    return a / b

# Obtener información de la excepción
try:
    dividir(4, 0)
except Exception as e:
    print(type(e).__name__, str(e))

Micro‑kata (7–10 min)

  • Implementa leer_entero(prompt) que repita hasta obtener un entero válido.
  • Escribe validar_email(email) que lance ValueError con mensajes claros.

Cheatsheet de excepciones

  • Bloques: try/except/else/finally
  • Tipos comunes: ValueError, TypeError, FileNotFoundError, ZeroDivisionError
  • raise para lanzar una excepción intencionalmente
  • Captura selectiva por tipo para respuestas adecuadas

Errores frecuentes

  • Capturar Exception demasiado pronto oculta problemas.
  • Silenciar errores sin log o mensaje dificulta depuración.
  • No cerrar recursos (usa with o finally).

Prueba/ejecución (opcional)

  • Ejecuta Basic/12_exceptions.py para ver las demos.

Material de apoyo

  • Código de referencia: Basic/12_exceptions.py

Qué te llevas hoy

  • Manejas errores con criterio y escribes código más resistente.

Siguiente paso

  • Clase 13: módulos e imports (organizar tu proyecto y reutilizar código).

⬅️ Anterior: Clase 11 - Clases | ⏭️ Siguiente: Clase 13 - Módulos | 🏠 Volver al Índice

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.