[Python-ideas] suggestion for try/except program flow

Mark Donald foobarmus at gmail.com
Sat Mar 28 15:54:33 CET 2009


Unless something has changed in Python 3+, I believe Nick's idiom
requires the generic handler code to be copied into a second except
clause to achieve identical behaviour, as follows...

except BaseException as e:
    if isinstance(e, Cheese):
        # handle cheese
    # handle all manner of stuff, including cheese
except:
    # handle all manner of OTHER stuff in the same way

...which makes the nested try block more semantic. This can be tested
by putting > raise "this is deprecated..."  into the try block, which
generates an error that is NOT caught by "except BaseException"
however IS caught by "except".

I realise that "recatch" may seem like a trivial suggestion, but I
believe it would be a slight improvement, which means it's not
trivial. Try/except statements are supposedly preferential to
"untried" if statements that handle isolated cases, but if statements
are a lot easier to imagine, so people use them all them time - style
be damned. Each slight improvement in try/except is liable to make it
more attractive to coders, the end result being better code (for
example, the implementation of PEP 341 increased my team's use of
try/except significantly, causing a huge improvement to code
readability).

Imagine if you could do this:

    runny = True
    runnier_than_you_like_it = False
    raise Cheese
except Cheese, e
    # handle cheese
    if runny: recatch as Camembert
except Camembert, e
    # handle camembert
    if runnier_than_you_like_it:
        recatch
    else:
        uncatch # ie, else clause will be effective...
except:
    # not much of a cheese shop, really is it?
else:
    # negotiate vending of cheesy comestibles
finally:
    # sally forth

And, I'm not trying to be belligerent here, but before somebody says...

Cheese(Exception)
Camembert(Cheese)

...just please have a little think about it.

Mark

2009/3/28 Guido van Rossum <guido at python.org>:
> On Sat, Mar 28, 2009 at 5:06 AM, Mark Donald <foobarmus at gmail.com> wrote:
>> In this situation, which happens to me fairly frequently...
>>
>> try:
>>  try:
>>     raise Cheese
>>  except Cheese, e:
>>     # handle cheese
>>     raise
>> except:
>>  # handle all manner of stuff, including cheese
>>
>> ...it would be nice (& more readable) if one were able to recatch a
>> named exception with the generic (catch-all) except clause of its own
>> try, something like this:
>>
>> try:
>>  raise Cheese
>> except Cheese, e:
>>  # handle cheese
>>  recatch
>> except:
>>  # handle all manner of stuff, including cheese
>
> I'm not sure recatch is all that more reasonable -- it's another
> fairly obscure control flow verb. I think the current situation isn't
> so bad. Nick already pointed out an idiom for doing this without two
> try clauses:
>
> except BaseException as e:
>  if isinstance(e, Cheese):
>    # handle cheese
>  # handle all manner of stuff, including cheese
>
> --
> --Guido van Rossum (home page: http://www.python.org/~guido/)
>



More information about the Python-ideas mailing list