try:
n = 10
if n < 20:
raise ValueError("n must be at least 20") # Raising a ValueError with a message
except ValueError as e:
print("An error has occurred:", e)
finally:
print("Code completed.")
--------
Custom Exception error.
class CustomError(Exception):
pass
try:
raise CustomError("This is a custom error message.")
except CustomError as e:
print("An error has occurred:", e)
finally:
print("Code completed.")
No hay comentarios:
Publicar un comentario