Python uses a runtime exception model: when something goes wrong, the interpreter raises an exception object that travels up the call stack until it is caught or the program terminates. Unlike Java, Python draws no formal line between checked and unchecked exceptions — every exception is simply a subclass ofDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/HotCode2025/Print-Estoy-Cansado-Jefe-TercerSemestre/llms.txt
Use this file to discover all available pages before exploring further.
BaseException, and you decide which ones to handle. The result is a leaner syntax, but it puts the responsibility on you to be precise about which errors you expect.
The simplest guard is a bare
try / except block that catches any exception through the root Exception class. The variable after as gives you access to the exception message.Notice that execution continues after the
except block — print('seguimos...') always runs because the exception was caught and the program did not crash.Catching bare
Exception as a permanent solution is an anti-pattern. It silences errors you did not anticipate and makes debugging much harder. Treat it as a last-resort catch-all, not a first line of defence.Python evaluates
except clauses from top to bottom and stops at the first match. Ordering matters: put the most specific types first and leave a generic fallback at the bottom only if you truly need it.resultado = None
try:
a = int(input("Digite el primer numero: "))
b = int(input("Digite el segundo numero: "))
resultado = a / b
except TypeError as e:
print(f"TypeError - Ocurrio un error: {type(e)}")
except ZeroDivisionError as e:
print(f"ZeroDivisionError - Ocurrio un error: {type(e)}")
except Exception as e:
print(f"Exception - Ocurrio un error: {type(e)}")
print(f"El resultado es: {resultado}")
print("seguimos... ")
ValueErrorint("abc"))TypeErrorZeroDivisionErrorFileNotFoundErrorIndexErrorKeyErrorAttributeErrorNaming your exceptions precisely is a form of self-documentation. A function signature that declares
except ZeroDivisionError tells every reader exactly which failure mode it handles — no need to read the implementation details.else runs only when no exception was raised inside the try block. Use it for code that should run on the happy path but does not belong inside the try itself.finally runs unconditionally — whether an exception was raised, caught, or never happened at all. Use it for cleanup (closing files, releasing locks, etc.).from NumerosIgualesException import NumerosIgualesException
resultado = None
try:
a = int(input('Digite el primer número: '))
b = int(input('Digite el segundo número: '))
if a == b:
raise NumerosIgualesException('Son números iguales')
resultado = a / b
except TypeError as e:
print(f'TypeError - Ocurrió un error: {type(e)}')
except ZeroDivisionError as e:
print(f'ZeroDivisionError - Ocurrió un error: {type(e)}')
except Exception as e:
print(f'Exception - Ocurrió un error: {type(e)}')
else:
print("No se arrojó ninguna excepción")
finally: # Siempre se va a ejecutar
print("Ejecución de este bloque finally")
print(f'El resultado es: {resultado}')
print('seguimos...')
The
raise keyword here manually throws a custom exception before the division happens — covered in detail in the next section.You can create domain-specific exception types by subclassing
Exception. Give the class an __init__ that stores a human-readable message so callers can inspect it programmatically.Exception (not BaseException) so the class plays nicely with generic except Exception fallbacks.self.message (or call super().__init__(mensaje) to let the base class handle it).raise to throw the exception from anywhere — it immediately interrupts normal flow.