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

Guido van Rossum guido at python.org
Sat Mar 28 12:56:50 CET 2009


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