Exception handling in Python 3.x

Peter Otten __peter__ at web.de
Fri Dec 3 09:42:33 EST 2010


Steven D'Aprano wrote:

> Consider the following common exception handling idiom:
> 
> def func(iterable):
>     it = iter(iterable)
>     try:
>         x = next(it)
>     except StopIteration:
>         raise ValueError("can't process empty iterable")
>     print(x)
> 
> The intention is:
> 
> * detect an empty iterator by catching StopIteration;
> * if the iterator is empty, raise a ValueError;
> * otherwise process the iterator.
> 
> Note that StopIteration is an internal detail of no relevance whatsoever
> to the caller. Expose this is unnecessary at best and confusing at worst.

http://mail.python.org/pipermail/python-list/2010-October/1258606.html
http://mail.python.org/pipermail/python-list/2010-October/1259024.html



More information about the Python-list mailing list