Allow executing multiple `except` clauses

or “Allow to continue exception handling after one match”

Note:
I’ve been searching in PEPs or in this Ideas category but I couldn’t find a topic about the subject I’m going to introduce.
However I’m new to python discussions so sorry if I missed it.

Has a keyword such as continue except already been considered?
I think that it could greatly help exception handling when sub-classed exception add extra handling steps but doesn’t totally differ from their parent exception handling, in the following code for example:

class ExceptionA(Exception):
    pass

class ExceptionB(ExceptionA):
    pass

try:
    raise ExceptionB()
except ExceptionB:
    # handle B
    continue except
except ExceptionA:
    # handle A
    continue except
except Exception:
    # handle default

Currently the only workarounds I could find for such a behavior would be to nest try/except, only match Exception then use isintance, call other functions/methods (with or without a ExceptionHandler class) or wrap this code into a recursive function. But all of them have flaws.

1 Like

Couldn’t you catch the parent and then check isinstance there?

try:
    raise ExceptionB
except ExceptionA as e:
    if isinstance(e, ExceptionB):
        # ExceptionB specific code
    # Rest of exception handling code

I could! This is one of the workarounds I suggested.
But it feels a bit like bypassing the matching mechanism that except propose.

I read:

as only catching Exception and not catching ExceptionA.

I would not consider catching a base class and handling a subclass specifically to be bypassing the mechanism if most of the handling code is the same? Perhaps if you were doing this often with a plain except Exception.

Perhaps if you were doing this often with a plain except Exception.

That’s it, even ExceptionA shares handling code with Exception.
“often” isn’t the case but still it felt weird to do this on Exception

See I want to do something if any exception is raised, add a extra handling if this exception is ExceptionA and again extra handling if this exception is ExceptionB (but still do the other two). With isinstance this would look like:

try:
    raise ExceptionB()
except Exception as e:
    if isinstance(e, ExceptionB):
        # ExceptionB specific code
    if isinstance(e, ExceptionA):
        # ExceptionA specific code
    # default exception handling code

or mixing both worlds (that seems like the best solution in my case because extra handling for ExceptionB is only one line)

try:
    raise ExceptionB
except ExceptionA as e:
    if isinstance(e, ExceptionB):
        # ExceptionB specific code
    # handle ExceptionA
    # default exception handling code
except Exception as e:
    # default exception handling code

Also I get that my little use case, of course, isn’t sufficient to justify the adding of a new keyword to the language.
I was just surprised that no one ever suggested the idea, and that there is no “finally keyword but only is an exception is raised” (a clause that would be executed anyway, if any exception is raised).

try:
    try:
        do_something():
    except Exception:
        do_general_stuff()
        raise
except SubExceptionA:
    ...
except SubExceptionB:
    ...

You could consider the following pattern as well, although I agree it is also not very nice. I do think it is nice enough to not add a new keyword though.