[python] using try: finally: except

Carl Banks imbosol at aerojockey.invalid
Fri Jun 18 23:28:12 EDT 2004


Peter Hansen wrote:
> OKB (not okblacke) wrote:
> 
>> Carl Banks wrote:
>> 
>>>The right way is:
>>>
>>>    try:
>>>        try:
>>>            x = 'hello'
>>>        except:
>>>            print "oops"
>>>    finally:
>>>        y = 'world'
>>>        print x," ",y
>> 
>>       I seem to recall reading somewhere that this was a cop-out for some 
>> implementation reason.  Is there any word on when or if it's going to be 
>> remedied?  It seems unbearably ugly and unintuitive; one of the most 
>> irritating Python warts.
> 
> I recall differently.  I recall reading several times that since
> it is completely ambiguous what the programmer meant if both are
> specified together, Guido deliberately kept them separate so that
> one had to be very explicit about whether the finally was inside
> or outside the except.  The behaviour of the code is quite different
> depending on the order...


try...except and try...finally are really two completely different
statements with different purposes.  It's very unfortunate that try is
used for both of them.

Frankly, when you do a try...finally, you're not really trying.  In
the words of the Jedi Master: "Try not.  Do, or do not.  There is no
try."  Which is why I think it should rather be do...finally.


-- 
CARL BANKS                      http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work." 
          -- Parody of Mr. T from a Robert Smigel Cartoon



More information about the Python-list mailing list