Exception handling in Python 3.x

Paul Rubin no.email at nospam.invalid
Fri Dec 3 13:15:58 EST 2010


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> def func(iterable):
>     it = iter(iterable)
>     failed = False
>     try:
>         x = next(it)
>     except StopIteration:
>         failed = True
>     if failed:
>         raise ValueError("can't process empty iterable")
>     print(x)

Untested:

    from itertools import islice

    def func(iterable):
       xs = list(islice(iter(iterable), 1))
       if len(xs) == 0:
          raise ValueError(...)
       print xs[0]



More information about the Python-list mailing list