title: GeneratorExit in while loop
body: <p>I am not clear about the behavior of catching GeneratorExit in a while loop, here is my code:</p>
  
  <pre><code># python 
  Python 2.6.6 (r266:84292, Sep  4 2013, 07:46:00) 
  [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; def func():
  ...     while True:
  ...         try:
  ...             yield 9
  ...         except GeneratorExit:
  ...             print "Need to do some clean up."
  ... 
  &gt;&gt;&gt; g = func()
  &gt;&gt;&gt; g.next()
  9
  &gt;&gt;&gt; g.close()
  Need to do some clean up.
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  RuntimeError: generator ignored GeneratorExit
  </code></pre>
  
  <p>It appears when g.close() is called, GeneratorExit is catched because "Need to do some clean up." is printed, but I do not understand why there is a RuntimeError.</p>