This repository has been archived by the owner on Nov 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Proposal: native exception handling API #20
Comments
Do except blocks use __subclasscheck__? I have a vague memory of them maybe
skipping that for speed.
(I think the larger issue is whether these semantics are helpful, but the
technical details matter too.)
…On Thu, Jun 18, 2020, 23:42 Max Fischer ***@***.***> wrote:
As an alternative *or addition* to #5
<#5>, it is possible
to use Python's try-except natively:
try:
async with something:
something.raise_concurrent()except MultiError[A]:
print("Handled concurrent A")except MultiError[A, B] as err:
print("Handled concurrent A and B")except MultiError[B], MultiError[C] as err:
print("Handled concurrent B or C:", err)except A:
print("Handled non-concurrent A")
Note: See original comment in trio/#611
<python-trio/trio#611 (comment)>
for further context and details not related to the except API itself. The
approach has been implemented and tested in the usim simulator
<https://github.com/MaineKuehn/usim>.
------------------------------
Pros:
The primary advantage here is that exception group handling follows the
same rules as regular exception handling. The sequence of handlers, how
handlers are executed, and the scope in which they are executed, are the
same.
Users do not have to import additional tools *for catching* exceptions
(splitting etc. is not part of this proposal). Everything can be
encapsulated in the MultiError/ExceptionGroup. The only visible "trick"
is templating via Cls[ExcA, ExcB], which should be common knowledge due
to typing.
Cons:
There are no native "subhandlers". Handling a MultiError[A, B] just
provides that kind of object; separating the handling of its A and B
components must be done manually. Handling sub-exceptions may-or-may-not be
part of the MultiError itself, but requires nesting inside the except
class.
The implementation is more complicated in that a metaclass is required: A
clause except A: uses issubclass(type(exception), A), which requires
implementing type(A).__subclasscheck__. Outwards simplicity is traded for
inner complexity.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#20>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEU42CVAC52XV7KRYHNTFLRXMCGTANCNFSM4OCOIVEA>
.
|
Yes, I'm currently preparing a draft PR so that things can be discussed with code samples as well. I'd love to get feedback on the usability. |
maxfischer2781
changed the title
Proposal: native exception handling API
[WIP] native exception handling API
Jun 19, 2020
maxfischer2781
changed the title
[WIP] native exception handling API
Proposal: native exception handling API
Jun 19, 2020
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
As an alternative or addition to #5, it is possible to use Python's
try
-except
natively:Note: See original comment in trio/#611 for further context and details not related to the
except
API itself. The approach has been implemented and tested in the usim simulator.Pros:
The primary advantage here is that exception group handling follows the same rules as regular exception handling. The sequence of handlers, how handlers are executed, and the scope in which they are executed, are the same.
Users do not have to import additional tools for catching exceptions (splitting etc. is not part of this proposal). Everything can be encapsulated in the
MultiError
/ExceptionGroup
. The only visible "trick" is templating viaCls[ExcA, ExcB]
, which should be common knowledge due totyping
.Cons:
There are no native "subhandlers". Handling a
MultiError[A, B]
just provides that kind of object; separating the handling of itsA
andB
components must be done manually. Handling sub-exceptions may-or-may-not be part of theMultiError
itself, but requires nesting inside theexcept
class.The implementation is more complicated in that a metaclass is required: A clause
except A:
usesissubclass(type(exception), A)
, which requires implementingtype(A).__subclasscheck__
. Outwards simplicity is traded for inner complexity.The text was updated successfully, but these errors were encountered: