[Python-Dev] PEP 463: Exception-catching expressions

Ethan Furman ethan at stoneleaf.us
Sat Feb 22 00:36:02 CET 2014


On 02/21/2014 02:26 PM, Eric V. Smith wrote:
> On 2/21/2014 5:06 PM, Greg Ewing wrote:
>>> On 21 February 2014 13:15, Chris Angelico wrote:
>>>
>>>> Generator expressions require parentheses, unless they would be
>>>> strictly redundant.  Ambiguities with except expressions could be
>>>> resolved in the same way, forcing nested except-in-except trees to be
>>>> correctly parenthesized

There would be no ambiguity if only nested excepts are allowed.  If one wants to catch multiple exceptions from one 
expression, /and do something different for each one/, use the statement form as it's going to be clearer.  For example:

    try:
        value = 1/x
    except ZeroDivisionError:
        try:
            value = 1/default['denominator']
        except KeyError:
            value = NaN

is much cleaner as:

    value = 1/x except ZeroDivisionError: 1/default['denominator'] except KeyError: NaN

However, this:

    try:
       result = Parse(some_stuff)
    except MissingOperator:
       result = ...
    except InvalidOperand:
       result = ...
    except SomethingElse:
       result = ...

would not benefit from being condensed into a single expression

--
~Ethan~


More information about the Python-Dev mailing list