Exception handling in Python 3.x
Hrvoje Niksic
hniksic at xemacs.org
Fri Dec 3 09:15:46 EST 2010
Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> 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)
Not exactly what you're looking for, but another way to express the
above is:
def func(iterable):
for x in iterable:
break
else:
raise ValueError("can't process empty iterable")
print(x)
Otherwise, I completely agree that being unable to completely replace
the original exception is an annoyance at best.
More information about the Python-list
mailing list