def iter_except(func, exception, start=None):
    'Yield a function repeatedly until it raises an exception'
    try:
        if start is not None:
            yield start()
        while 1:
            yield func()
    except exception:
        pass
Yield a function repeatedly until it raises an exception
recipes/Python/577155_iterexcept__useful_variant/recipe-577155.py