Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- Ejemplos de excepciones:
- 1) Bloque completo:
- try:
- .... # intento hacer esto
- except .....:
- .... # ejecuto esta linea si try lanza el error de la linea 9
- else:
- ...... # ejecuto esto si no hay error
- finally:
- ...... # esto se ejecuta siempre, haya o no error
- 2) except acepta varias formas
- try:
- .....
- except (ValueError, NameError...):
- ...... # se ejecuta si alguno de los errores de la linea 19 sucede
- 3) try:
- .....
- except ValueError:
- print("ValueError")
- except NameError:
- print("NameError")
- 4) Si quiero capturar cualquier excepcion:
- try:
- .....
- except Exception:
- print("Excepción")
- o asi:
- try:
- .....
- except:
- print("Excepción")
- """
- """
- # Script que pide un numero no nulo y responde si este es positivo, negativo
- # o cero
- def ingresar_entero():
- while True:
- try:
- numero = int(input("Ingrese un nro: "))
- except ValueError:
- print("Debe ingresar un nro entero...")
- else:
- return numero
- numero = ingresar_entero()
- if numero > 0:
- print("Positivo")
- elif numero < 0:
- print("Negativo")
- else:
- print("Cero")
- """
- # como implementar excepciones con funciones (lanzar excepciones)
- def sumar(a,b):
- """Función que toma como argumentos dos nros a y b y devuelve su suma"""
- if not isinstance(a,(int,float)) or not isinstance(b,(int,float)):
- raise TypeError("Se requieren dos números")
- return a + b
- print(sumar(1,2))
- a =2
- b = 3
- print(sumar("a","b"))
- print([1,2,3],["Ana"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement