ATIX20

Page 51

def mi_generador(): try: for i in range(5): print 'Produciento', i yield i except GeneratorExit: print 'Terminando' g = mi_generador() print g.next() g.close() $ python exceptions_GeneratorExit.py Produciento 0 0 Terminando

IOError Se produce cuando la entrada o salida fallan, por ejemplo cuando un disco se llena o el archivo de entrada no existe. f = open('/no/existe', 'r') $ python exceptions_IOError.py Traceback (most recent call last): File "exceptions_IOError.py", line 12, in <module> f = open('/no/existe', 'r') IOError: [Errno 2] No such file or directory: '/no/existe'

ImportError Se produce cuando un módulo, o un miembro de un módulo, no pueden ser importados. Existen un par de condiciones en las que un ImportError se puede producir. ✔

Si el módulo no existe.

import modulo_no_existe $ python exceptions_ImportError_nomodule.py Traceback (most recent call last): File "exceptions_ImportError_nomodule.py", line 12, in <module> import modulo_no_existe ImportError: No module named modulo_no_existe ✔

Si from X import Y es usado y Y no puede ser encontrado dentro del módulo X, un ImportError es creado.

from exceptions import NombreInventado $ python exceptions_ImportError_missingname.py Traceback (most recent call last): File "exceptions_ImportError_missingname.py", line 12, in <module> from exceptions import NombreInventado ImportError: cannot import name NombreInventado


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.