teslariu

excepciones

Jul 30th, 2022
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #
  5. '''
  6. def ingresar_entero():
  7.    while True:
  8.        try:
  9.            numero = int(input("Ingrese un numero entero: "))
  10.        except ValueError:
  11.            print("No ha ingresado un nro entero")
  12.        else:
  13.            return numero
  14.        
  15. numero = ingresar_entero()
  16.        
  17. if numero:
  18.    print(f"El cociente entre 5 y {numero} es {5/numero}")
  19. else:
  20.    print("No se puede dividir por cero")
  21.    
  22. """
  23. try:
  24.    print(f"El cociente entre 5 y {numero} es {5/numero}")
  25. except ZeroDivisionError:
  26.    print("No se puede dividir por cero")
  27. """
  28. '''
  29. # LANZAMIENTO DE EXCEPCIONES
  30. def sumar(a,b):
  31.     """Función que suma dos numeros"""
  32.     if not isinstance(a,(int,float)) or not isinstance(b,(int,float)):
  33.         raise TypeError("Se necesitan dos nros")
  34.     return a + b
  35.  
  36.  
  37. #print(sumar("Hola","Chau"))
  38. print(sumar(2,3))
  39.  
  40. """
  41. #simulacion de conexion a internet
  42. try:
  43.    # conexion a internet
  44. except (Error1, Error2):
  45.    # print("Error de conexión")
  46. else:
  47.    # hago mi codigo
  48. finally:  --> se ejecuta siempre, haya o no excepcion
  49.    # cerrar la conexion
  50. """
  51.  
Advertisement
Add Comment
Please, Sign In to add comment