Introducing try..except* #4
Description
The design discussed in this issue has been consolidated in https://github.com/python/exceptiongroups/blob/master/except_star.md.
Disclaimer
- I'm going to be using the
ExceptionGroupname in this issue, even though there are other alternatives, e.g.AggregateException. Naming of the "exception group" object is outside of the scope of this issue. - This issue is primarily focused on discussing the new syntax modification proposal for the
try..exceptconstruct, shortly called "except*". - I use the term "naked" exception for regular Python exceptions not wrapped in an ExceptionGroup. E.g. a regular
ValueErrorpropagating through the stack is "naked". - I assume that
ExceptionGroupwould be an iterable object. E.g.list(ExceptionGroup(ValueError('a'), TypeError('b')))would be equal to[ValueError('a'), TypeError('b')] - I assume that
ExceptionGroupwon't be an indexable object; essentially it's similar to Pythonset. The motivation for this is that exceptions can occur in random order, and letting users writegroup[0]to access the "first" error is error prone. The actual implementation ofExceptionGroupwill likely use an ordered list of errors though. - I assume that
ExceptionGroupwill be a subclass ofBaseException, which means it's assignable toException.__context__and can be directly handled withexcept ExceptionGroup. - The behavior of good and old regular
try..exceptwill not be modified.
Syntax
We're considering to introduce a new variant of the try..except syntax to simplify working with exception groups:
try:
...
except *SpamError:
...
except *BazError as e:
...
except *(BarError, FooError) as e:
...The new syntax can be viewed as a variant of the tuple unpacking syntax. The * symbol indicates that zero or more exceptions can be "caught" and processed by one except * clause.
We also propose to enable "unpacking" in the raise statement:
errors = (ValueError('hello'), TypeError('world'))
raise *errorsSemantics
Overview
The except *SpamError block will be run if the try code raised an ExceptionGroup with one or more instances of SpamError. It would also be triggered if a naked instance of SpamError was raised.
The except *BazError as e block would aggregate all instances of BazError into a list, wrap that list into an ExceptionGroup instance, and assign the resultant object to e. The type of e would be ExceptionGroup[BazError]. If there was just one naked instance of BazError, it would be wrapped into a list and assigned to e.
The except *(BarError, FooError) as e would aggregate all instances of BarError or FooError into a list and assign that wrapped list to e. The type of e would be ExceptionGroup[Union[BarError, FooError]].
Even though every except* star can be called only once, any number of them can be run during handling of an ExceptionGroup. E.g. in the above example, both except *SpamError: and except *(BarError, FooError) as e: could get executed during handling of one ExceptionGroup object, or all of the except* clauses, or just one of them.
It is not allowed to use both regular except clauses and the new except* clauses in the same try block. E.g. the following example would raise a SyntaxErorr:
try:
...
except ValueError:
pass
except *CancelledError:
passExceptions are mached using a subclass check. For example:
try:
low_level_os_operation()
except *OSerror as errors:
for e in errors:
print(type(e).__name__)could output:
BlockingIOError
ConnectionRefusedError
OSError
InterruptedError
BlockingIOError
New raise* Syntax
The new raise * syntax allows to users to only process some exceptions out of the matched set, e.g.:
try:
low_level_os_operation()
except *OSerror as errors:
new_errors = []
for e in errors:
if e.errno != errno.EPIPE:
new_errors.append(e)
raise *new_errorsThe above code ignores all EPIPE OS errors, while letting all others propagate.
raise * syntax is special: it effectively extends the exception group with a list of errors without creating a new ExceptionGroup instance:
try:
raise *(ValueError('a'), TypeError('b'))
except *ValueError:
raise *(KeyError('x'), KeyError('y'))
# would result in:
# ExceptionGroup({KeyError('x'), KeyError('y'), TypeError('b')})A regular raise would behave similarly:
try:
raise *(ValueError('a'), TypeError('b'))
except *ValueError:
raise KeyError('x')
# would result in:
# ExceptionGroup({KeyError('x'), TypeError('b')})raise * accepts arguments of type Iterable[BaseException].
Unmatched Exceptions
Example:
try:
raise *(ValueError('a'), TypeError('b'), TypeError('c'), KeyError('e'))
except *ValueError as e:
print(f'got some ValueErrors: {e}')
except *TypeError as e:
print(f'got some TypeErrors: {e}')
raise *eThe above code would print:
got some ValueErrors: ExceptionGroup({ValueError('a')})
got some TypeErrors: ExceptionGroup({TypeError('b'), TypeError('c')})
And then crash with an unhandled KeyError('e') error.
Basically, before interpreting except * clauses, the interpreter will have an exception group object with a list of exceptions in it. Every except * clause, evaluated from top to bottom, can filter some of the exceptions out of the group and process them. In the end, if the exception group has no exceptions left in it, it wold mean that all exceptions were processed. If the exception group has some unprocessed exceptions, the current frame will be "pushed" to the group's traceback and the group would be propagated up the stack.
Exception Chaining
If an error occur during processing a set of exceptions in a except * block, all matched errors would be put in a new ExceptionGroup which would have its __context__ attribute set to the just occurred exception:
try:
raise *(ValueError('a'), ValueError('b'), TypeError('z'))
except *ValueError:
1 / 0
# would result in:
#
# ExceptionGroup({
# TypeError('z'),
# ZeroDivisionError()
# })
#
# where the `ZeroDivizionError()` instance would have
# its __context__ attribute set to
#
# ExceptionGroup({
# ValueError('a'), ValueError('b')
# })It's also possible to explicitly chain exceptions:
try:
raise *(ValueError('a'), ValueError('b'), TypeError('z'))
except *ValueError as errors:
raise RuntimeError('unexpected values') from errors
# would result in:
#
# ExceptionGroup(
# TypeError('z'),
# RuntimeError('unexpected values')
# )
#
# where the `RuntimeError()` instance would have
# its __cause__ attribute set to
#
# ExceptionGroup({
# ValueError('a'), ValueError('b')
# })See Also
- An analysis of how exception groups will likely be used in asyncio programs: Introducing try..catch #3 (comment)
- A WIP implementation of the
ExceptionGrouptype by @iritkatriel tracked here: GitHub - iritkatriel/cpython at exceptionGroup
Activity
gvanrossum commentedon Oct 26, 2020
Very nice! Only a few nits.
Why do we need
raise *errors? What's wrong withraise ExceptionGroup(*errors)? I think that should have the same semantics.I think your English description is less precise than my example in #3 (comment). For example you don't specify in which order multiple handlers are run if more than one is run (mine makes it clear that they run from top to bottom) and I also think we should be careful to clarify what happens if you write e.g.
(To get the desired effect, the more specific handler should come first.)
Another thing: when an
ExceptionGroupwraps anotherExceptionGroup, iterating over the outer group should recurse into the inner group (i.e. flatten the structure); the matching process should do this too.Shouldn't this use bare
raise?Also probably should link to python-trio/trio#611 (Trio MultiError v2).
iritkatriel commentedon Oct 26, 2020
Awesome!
This could sound like
eends up being a list. Maybe just say "it would be wrapped by an ExceptionGroup" (that inside it's in a list is an implementation detail). The following paragraph can be probably simplified a bit in the same way.It's the other way around (as in your example) - the exception that just happened has context which is the ExceptionGroup that we are now handling.
It might be worth reiterating here what happens to exceptions raised within the except clauses (they are added to a separate list and merged with the unhandled exceptions at the end so they cannot be handled by another except clause of this try.)
We considered (but I don't think we've decided yet) to make it a new type which is a superclass of
BaseException, so thatexcept BaseExceptiondoesn't catch it.Still open: How control flow statements (return, continue, break) interact with a try-*except clause. Return should take us to
finally. For continue/break I can think of more than one option.1st1 commentedon Oct 26, 2020
@gvanrossum:
Actually yes, I now think so too. Let me shed some light on why I added this syntax.
In the initial version of this proposal, the type of
einexcept *ValueError as e:would belist[ValueError]. I wanted to hideExceptionGroupfrom the user as much as possible. Then I realized that we also need to make__cause__work, and that implies making it possible to writeraise e from Noneetc. So I had to fixlist[ValueError]toExceptionGroup[ValueError].Second, quoting the proposal:
Basically, I wanted
raise *errorsto merge all errors back into theExceptionGroupwe're handling now. I now don't think it's a good idea: it's hard to explain to users and the distinction between the behavior ofraise *errorsandraise ExceptionGroup(*errors)would be utterly confusing.So yeah, let's remove
raise *until we find good motivation to include something like it. I'll update the proposal.Right, I'll add a variant of this example to the proposal.
Now this is something I'm really not sure about.Oh, I was just about to make an argument against this, when it struck me that you're right.If there's a
KeyboardInterruptexception buried in some deeply nestedExceptionGroupyou'd still want to catch it. I'll clarify this all in the proposal.Yes. This is a remnant of my
list[ValueError]idea; I'll fix this.@iritkatriel
Good catch! If you see my reply to @gvanrossum you'll find why ;) I'll fix this.
Another great catch, will fix.
Alright, I'll clarify.
Since they
ExceptionGroupobjects need to be able to be referenced from__cause__and__context__I think it's inevitable that they must be part of the exceptions class hierarchy. IMO, at the very least we'll need to introduce anotherBaseBaseExceptionto it to avoidExceptionGroupextendingBaseException.I'll add examples.
gvanrossum commentedon Oct 26, 2020
I would prefer not to do that. I will be backwards incompatible, since there is likely tons of code out there that either catches
BaseExceptionor usesisinstance(x, BaseException). Such code assumes that it'll get all exceptions; if it gets aBaseBaseExceptionthat isn't aBaseExceptiontheir assumptions will be violated.I know @njsmith said he would like
ExceptionGroupto be something completely different from exceptions, but I think that's going too far. In the end it is an exception, is treated by an exception by all of the runtime, and you should be able to catch it withexcept BaseException(no*).User code may have no business interacting with
ExceptionGroup, but people will have to write infrastructure code in Python that does have to interact with it (e.g. custom loggers) so it should just be exposed as anotherBaseExceptionwith a custom API.1st1 commentedon Oct 26, 2020
I'm also not entirely sure why we'd need that. If we make it completely different from exceptions then
ExceptionGroupwould still need to be duck-type compatible withBaseExceptionon both Python & C level just so that everything works when it is assigned to__context__and__cause__of regular exceptions.And I also don't see a good enough argument to deepen the Python exceptions hierarchy with
BaseBaseException. IMO, makingExceptionGroupaBaseExceptionis good enough. Moreover, logging code likeis better to continue working when exception groups are around.
gvanrossum commentedon Oct 26, 2020
How bad would it be if we just included these in the translated code, assuming the translated code is something like
Noting that there actually must be another try statement to deal with exceptions coming out of
<handler>(they must be added to the separate list which will be merged with_errat the end), but that shouldn't affectbreak/continue/return. (This should probably be done using a try..finally around all the handlers together.)However, another way to think of these is that they raise a special kind of pseudo-exception that isn't catchable but that, when it is re-raised at the end of the
finallyblock, magically executes the desired control flow operation. This is how these are currently handled (though I don't recognize the code in ceval enough to be able to point to it without more research). Then however we have a problem if both except blocks get run in e.g.Which will it do? The first semantics I propose will clearly do
breakbecause at that point the handler for E2 will not even be called. But is that what we expect?iritkatriel commentedon Oct 26, 2020
Does the first option mean that break/continue acts on a loop enclosing this try-except block? So either break or continue means that no further except clauses are executed (but finally of course is)?
1st1 commentedon Oct 26, 2020
How about we prohibit
continueandbreakinsideexcept*? We can enable them in later versions of Python. But right now, it seems it's very hard to define them in a non-confusing way.aeros commentedon Oct 26, 2020
Agreed. I think that for now,
continueandbreakadd a significant degree of complexity that hasn't been defined to be practically useful enough within exception groups (at least not as of writing this, legitimate use cases may emerge as it matures). Even with those disabled, I think we're already hitting a threshold where the complexity is going to be a lot to digest for a single PEP.gvanrossum commentedon Oct 26, 2020
If we forbid break/continue we should also disallow return, which has the same issues. But while break/continue in except blocks feel pretty academic, return does not.
Initial version of the except* proposal
1st1 commentedon Oct 27, 2020
I decided that it would be much more productive to use GitHub PRs to start tracking changes to the proposal. I've created a document combining my motivation for the
try..except*syntax as well as the opening of this issue in one file. And here's the first PR addressing feedback from @iritkatriel and @gvanrossum. Maybe we'll gradually morph that file into the PEP, we'll see.1st1 commentedon Oct 27, 2020
The PR: #5
Reference issue #4 from See Also
1st1 commentedon Oct 28, 2020
I'm closing this issue for now. We'll open new ones to discuss specific aspects of the proposal, that now lives at https://github.com/python/exceptiongroups/blob/master/except_star.md.