[Python-ideas] syntax to continue into the next subsequent except block

Steven D'Aprano steve at pearwood.info
Mon Sep 17 13:24:21 CEST 2012

On 17/09/12 20:43, Paul Wiseman wrote:

> with your ValueError case, say you wanted to call ham on every case of a
> parent exception to ValueError (I guess it could only be Exception for this
> example), but not ValueError where condition was true,
> so how would you write this without the except..if?
> try:
>      something()
> except HTTPError as ex if condition(ex):
>      spam()
> except URLError:
>      # if not condition, fall through to the next except block
>      ham()

except URLError as ex:  # will also catch HTTPError
     # if you want to be explicit, say "except HTTPError, URLError as ex"
     if isinstance(ex, HTTPError) and condition(ex):

> (I switched ValueError for HTTPError because it has more parent classes
> that just Exception, because having 'except Exception' probably isn't the
> greatest example)

Sure, no worries.


More information about the Python-ideas mailing list