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

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Feb 22 02:55:16 CET 2014

Steven D'Aprano wrote:

>     result = computation(
>                  int(arg) except ValueError: abort("Invalid int")
>                  )
> Actually, not quite so nice as I first thought, since you're relying on 
> the side-effects of abort() rather than returning a value.

Yeah, while I was writing that I wondered whether
you should be allowed to write

    int(arg) except ValueError: raise UserError("Invalid int")

That looks heretical, because 'raise' can't in any
way be interpreted as a value-returning expression.
But you can achieve the same result using a function
that always raises and exception, so forbidding it
on those grounds would be pointless.

And writing it that way at least makes it obvious that
it *does* always raise an exception, in the same way

        i = int(arg)
     except ValueError:
        raise UserError("Invalid int")
        result = computation(i)

makes it obvious that control can't fall off the
end of the except branch.


More information about the Python-Dev mailing list