def echo(value=None): print "Execution starts when 'next()' is called for the first time." try: while True: try: print "Before yield" value = (yield value) print "After yield" except GeneratorExit: print "In GeneratorExit" raise StopIteration except Exception, e: print "In Exception" value = e finally: print "Don't forget to clean up when 'close()' is called."