generator exceptions

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Sep 19 12:07:02 EDT 2008


En Fri, 19 Sep 2008 10:40:00 -0300, Alexandru Mosoi <brtzsnr at gmail.com>  
escribió:

> i have a generator that raises an exception when calling next(),
> however if I try to catch the exception and print the traceback i get
> only the line where next() was called
>
>
> while True:
>   try:
>     iterator.next()
>   except StopIteration:
>     break
>   except Exception, e:
>     traceback.print_exc()
>
> how do I get the traceback starting from where exception was raised in
> first place?

I get a complete traceback:

py> import traceback
py>
py> def a(x):
...     raise ValueError
...
py> def b(x):
...     return a(x)
...
py> def c(n):
...     for i in range(n):
...         yield b(i)
...
py> it = c(5)
py> while True:
...   try:
...     it.next()
...   except StopIteration:
...     break
...   except Exception:
...     print traceback.print_exc()
...
Traceback (most recent call last):
   File "<stdin>", line 3, in <module>
   File "<stdin>", line 3, in c
   File "<stdin>", line 2, in b
   File "<stdin>", line 2, in a
ValueError
None
py>

Could you provide a more complete example that fails?

-- 
Gabriel Genellina




More information about the Python-list mailing list