- Notifications
You must be signed in to change notification settings - Fork 0
15. Errores en Python
Daniel Herrera Sánchez edited this page Jan 12, 2020
·
1 revision
importsys#creemos nuestro propio error# cantidad_pasajeros = 10# if cantidad_pasajeros > 5:# raise Exception(# 'la cantidad de pasajeros no debe ser mayor a 5 usted ingresó {} '# .format(cantidad_pasajeros))# Definir que solo corre sobre un sistema operativo# assert ('linux' in sys.platform), "Este código corre solo en linux."# try exceptdeflinux_interaction():
assert ('linux'insys.platform), "Esta fúncion solo se ejecutará en Linux."print('Si eres Linux este mensaje aparecerá en pantalla.')
try:
linux_interaction()
exceptAssertionErroraserror:
print(error)
print('La función linux_interaction() no fue ejecutada')
#Y si no existe un archivo?try:
withopen('file.log') asfile:
read_data=file.read()
except:
print('no se puedo encontrar el archivo file.log')
#O podemos mostrar el error original detectado por pythontry:
withopen('file.log') asfile:
read_data=file.read()
exceptFileNotFoundErrorasfnf_error:
print(fnf_error)
#uniendo los dos casostry:
linux_interaction()
withopen('file.log') asfile:
read_data=file.read()
exceptFileNotFoundErrorasfnf_error:
print(fnf_error)
exceptAssertionErroraserror:
print(error)
print('La función linux_interaction() no fue ejecutada')
#la estructura finally se ejecutará al final sin importar quetry:
linux_interaction()
exceptAssertionErroraserror:
print(error)
else:
try:
withopen('file.log') asfile:
read_data=file.read()
exceptFileNotFoundErrorasfnf_error:
print(fnf_error)
finally:
print('Esto se ejecuta siempre')