catching exceptions from an except: block

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Mar 7 22:55:54 CET 2007


Miki a écrit :
> Hello Arnaud,
> 
> 
>>Imagine I have three functions a(x), b(x), c(x) that each return
>>something or raise an exception.  Imagine I want to define a function
>>that returns a(x) if possible, otherwise b(x), otherwise c(x),
>>otherwise raise CantDoIt.
> 
> Exceptions are for error handling, not flow control.

def until(iterable,sentinel):
   for item in iterable:
     if item == sentinel:
       raise StopIteration
     yield item

 >>> for item in until(range(10), 5):
...     print item
...
0
1
2
3
4
 >>>

Exceptions *are* a form of flow control.



More information about the Python-list mailing list