Does Python do try: except: finally: ?

Anton Muhin antonmuhin at sendmail.ru
Thu Mar 20 06:53:26 EST 2003


Erik Max Francis wrote:
> Joseph Wilhelm wrote:
> 
> 
>>>>>try:
>>
>>...     print "Foo"
>>... except:
>>...     print "Oof"
>>... else:
>>...     print "Bar"
>>...
>>Foo
>>Bar
>>
>>That what you need?
> 
> 
> Probably not:
> 
> 
>>>>try:
> 
> ....  print 'foo'
> ....  raise ValueError
> .... except:
> ....  print 'oof'
> .... else:
> ....  print 'bar'
> .... 
> foo
> oof
> 
> The else clause is only executed in the case where there's no error,
> quite contrary to what is usually intended by a try..except...finally
> clause.
> 

It can be easily improved:
try:
     print "foo"
     raise ValueError
except:
     print "oof"
else:
     print "bar"
print "zoo" #

Anton.





More information about the Python-list mailing list