[Python-Dev] Pre-PEP: Unifying try-except and try-finally

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Fri May 6 18:00:46 CEST 2005


Guido van Rossum wrote:
> [François Pinard]
>> It happens once in a while that I want to comment out the except clauses
>> of a try statement, when I want the traceback of the inner raising, for
>> debugging purposes.  Syntax forces me to also comment the `try:' line,
>> and indent out the lines following the `try:' line.  And of course, the
>> converse operation once debugging is done.  This is slightly heavy.
> 
> I tend to address this by substituting a different exception. I don't
> see the use case common enough to want to allow dangling try-suites.

Easy enough, adding "raise" at the top of the except clause also solves the problem.

>> P.S. - Another detail, while on this subject.  On the first message I've read
>> on this topic, the original poster wrote something like:
>> 
>>     f = None
>>     try:
>>         f = action1(...)
>>     ...
>>     finally:
>>         if f is not None:
>>             action2(f)
>> 
>> The proposed syntax did not repeat this little part about "None", quoted
>> above, so suggesting an over-good feeling about syntax efficiency.
>> While nice, the syntax still does not solve this detail, which occurs
>> frequently in my experience.  Oh, I do not have solutions to offer, but
>> it might be worth a thought from the mighty thinkers of this list :-)
> 
> I don't understand your issue here. What is the problem with that
> code? Perhaps it ought to be rewritten as
> 
> f = action1()
> try:
>     ...
> finally:
>     action2(f)
> 
> I can't see how this would ever do something different than your version.

Well, in the original the call to action1 was wrapped in an additional try-except
block.

f = None
try:
    try:
        f = action1()
    except:
        print "error"
finally:
    if f is not None:
        action2(f)


Reinhold


-- 
Mail address is perfectly valid!



More information about the Python-Dev mailing list