Apuntes_Python/01_curso/Modulo_2/2-2a_excepeciones.py
2022-12-24 22:41:20 -03:00

107 lines
2.0 KiB
Python

print(' Hay principalmente 2 tipos de errores: de sintaxis y excepciones ')
while True :
try:
x = int(input('Excepcion si no se ingresa un ENTERO ->'))
break
except ValueError:
print('Se puede salir con Ctrl-C')
while True :
try:
x = int(input('Excepcion si no se ingresa un ENTERO ->'))
break
except (ValueError, KeyboardInterrupt):
print('Ya, no se puede salir y con Ctrl-C')
print('''
... except (RuntimeError, TypeError, NameError):
... pass
''')
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
for cls in [B, C, D]:
try:
raise cls()
except D:
print("D", D)
except C:
print("C", C)
except B:
print("B", B)
print('')
for cls in [B, C, D]:
try:
raise cls()
except B:
print("B")
except D:
print("D")
except C:
print("C")
print('')
import sys
try:
f = open('mifile.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("Error OS: {0}".format(err))
except ValueError:
print("No pude convertir el dato a entero.")
except:
print("Error inesperador: ", sys.exc_info()[0])
raise
'''
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except OSError:
print('no pude abrir', arg)
else:
print(arg, 'tiene', len(f.readlines()), 'lineas')
f.close()
'''
try:
raise Exception('carne', 'huevos')
except Exception as inst:
print(type(inst))
print(inst.args)
print(inst)
#x, y = inst
#print('x: ',x ,'y: ',y)
"""
>>> raise RuntimeError('Error error bip bap bip! no computa! bip bap!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Error error bip bap bip! no computa! bip bap!
"""
# RELANZANDO la EXCEPcion para se manejada en otro contexto.
try:
raise NameError('y bueno un error común')
except NameError:
print('ee.. salto un except!')
raise