generator exceptions

A

Alexandru Mosoi

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?
 
M

Michael Palmer

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?

What happens if you simply remove the 'except Exception' clause? It
should print the entire traceback by default.
 
G

Gabriel Genellina

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top