generator exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandru  Mosoi

    generator exceptions

    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?
  • Michael Palmer

    #2
    Re: generator exceptions

    On Sep 19, 9:40 am, Alexandru Mosoi <brtz...@gmail. comwrote:
    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.

    Comment

    • Gabriel Genellina

      #3
      Re: generator exceptions

      En Fri, 19 Sep 2008 10:40:00 -0300, Alexandru Mosoi <brtzsnr@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:

      pyimport traceback
      py>
      pydef a(x):
      .... raise ValueError
      ....
      pydef b(x):
      .... return a(x)
      ....
      pydef c(n):
      .... for i in range(n):
      .... yield b(i)
      ....
      pyit = c(5)
      pywhile 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

      Comment

      Working...