def iter_except(func, exception, first_=None):
    """ Call a function repeatedly until an exception is raised.

    Converts a call-until-exception interface to an iterator interface.
    Like __builtin__.iter(func, sentinel) but uses an exception instead
    of a sentinel to end the loop.

    Examples:
        # priority queue iterator
        iter_except(functools.partial(heappop, h), IndexError)

        # non-blocking dict iterator
        iter_except(d.popitem, KeyError)

        # non-blocking deque iterator
        iter_except(d.popleft, IndexError)

        # loop over a producer Queue
        iter_except(q.get_nowait, Queue.Empty)

        # non-blocking set iterator
        iter_except(s.pop, KeyError)

    http://docs.python.org/3.4/library/itertools.html#itertools-recipes
    """
    try:
        if first_ is not None:
            yield first_()
        while 1:
            yield func()
    except exception:
        pass
