Le 03/03/2021 à 23:49, Irit Katriel via Python-Dev a écrit :
> 
> On Wed, Mar 3, 2021 at 10:39 PM Greg Ewing <greg.ew...@canterbury.ac.nz
> <mailto:greg.ew...@canterbury.ac.nz>> wrote:
> 
>     [...]
>     In other words, the only difference between except and
>     except* would be that multiple except* clauses can be run,
>     whereas only one except clause will run (the first one that
>     matches something in the ExceptionGroup).
> 
>     Is there any good reason not to do things that way?
> 
> That's an interesting idea. 
> 
> Do you mean that one exception gets handled and the rest of the group is
> reraised? Or discarded?
> [...]

Hi,

I'll take a shoot at this, just to see how it tastes… So, let's say:

When an exception group reaches a set of traditional "except" clauses,
those are examined one after the other, in the order they are in the
code. That way, exceptions matched by several clauses will cause the
first one to run, same as today.

A subgroup is built with the subset of exceptions matched by the
examined clause, as the PEP specifies for "except*". If this subgroup is
None, the clause is not selected, and the next clause, if any, is
examined. On the contrary, if the subgroup contains at least one matched
exception, the clause is selected and no other clause will run (again,
same as today). Exceptions not part of the subgroup are discarded.

The clause body is then run just once (so the boss only gets one email
about KeyboardInterrupt). If the clause uses the "as" form, the "as"
variable is bound to one exception in the subgroup, which one is
unspecified (at least for now). The other ones are discarded, except if
a bare "raise" is reached (below).

If a bare "raise" is reached while executing the body, the selected
subgroup propagates out of the "try-except" construct. Justification:
the whole group cannot propagate, because today a bare "raise" cannot
reraise exceptions of a type not matched by the clause. However, if a
single-type exception group is handled similar to a single exception in
traditional "except" clauses, it is acceptable to let it propagate.

So you would have:

try:
    g=BaseExceptionGroup(
        [ValueError(), KeyboardInterrupt(), KeyboardInterrupt()])
    raise g
except RuntimeError:               # doesn't match
    log_the_error()
except  KeyboardInterrupt as e: # builds s=g.subgroup(KeyboardInterrupt)
    email_the_boss(e)           # tells the boss of any one error
    raise                       # reraises s
except BaseException:           # would match, but doesn't run
    launch_nuclear_attack()

# BaseExceptionGroup([KeyboardInterrupt(), KeyboardInterrupt()])
# propagates further, a traditional "except KeyboardInterrupt"
# would catch it. The ValueError is discarded.

An interesting feature would be: when the matching clause has no "as",
"except" behaves the same as "except*", apart from the fact that only
one clause may run.

Cheers,
Baptiste
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/EXC6BQVHQXUXBHIEWHLSLU6FY7SJKIF3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to