Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 30th, 2012  |  syntax: Python  |  size: 0.49 KB  |  views: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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)))
clone this paste RAW Paste Data