SHARE
TWEET

Untitled

a guest Nov 30th, 2012 57 Never
  1. #!/usr/bin/python
  2.  
  3. def raisinggenfunc():
  4.   yield 1
  5.   raise ValueError("spurious error")
  6.   yield 3
  7.  
  8. def wrapper(gen):
  9.   while True:
  10.     try:
  11.       value = next(gen)
  12.     except StopIteration:
  13.       raise
  14.     except Exception as e:
  15.       print(e)
  16.     else:
  17.       yield value
  18.  
  19. def raiseon2(i):
  20.   if i == 2:
  21.     raise ValueError("spurious error")
  22.   return i
  23.  
  24. raisinggen = (raiseon2(i) for i in range(1, 4))
  25.  
  26. print(list(wrapper(raisinggenfunc())))
  27. print(list(wrapper(raisinggen)))
RAW Paste Data
Top