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

Ethan Furman ethan at stoneleaf.us
Fri Feb 21 19:19:19 CET 2014


On 02/20/2014 07:15 PM, Chris Angelico wrote:
> PEP: 463
> Title: Exception-catching expressions
>
> [snip]
>
> Deferred sub-proposals
> ======================
>
> Multiple except clauses
> -----------------------
>
> An examination of use-cases shows that this is not needed as often as
> it would be with the statement form, and as its syntax is a point on
> which consensus has not been reached, the entire feature is deferred.
>
> In order to ensure compatibility with future versions, ensure that any
> consecutive except operators are parenthesized to guarantee the
> interpretation you expect.
>
> Multiple 'except' keywords can be used, and they will all catch
> exceptions raised in the original expression (only)::
>
>      # Will catch any of the listed exceptions thrown by expr;
>      # any exception thrown by a default expression will propagate.
>      value = (expr
>          except Exception1 [as e]: default1
>          except Exception2 [as e]: default2
>          # ... except ExceptionN [as e]: defaultN
>      )

-1

If one wants to catch multiple exceptions from the same original operation, use the statement block.  Keep the exception 
expression syntax simple and chained, akin to the if..else syntax:

value = expr1 except exc1 expr2 except exc2 expr3 ...

So each following except is only catching exceptions from the last expression (in other words, exc1 only from expr1, 
exc2 only from expr2, etc.)  Keep it simple.  If one wants the power of the statement form, use the statement syntax.

--
~Ethan~


More information about the Python-Dev mailing list