This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Except multiple types of exceptions with OR keyword is not working
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ShlomiRex, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-09-09 14:35 by ShlomiRex, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg401486 - (view) Author: Shlomi (ShlomiRex) Date: 2021-09-09 14:35
When I want to catch multiple types of exceptions naturally 'OR' keyword is used. But it doesn't work. The interpreter doesn't show any error for the syntax, so developer may think it would work.

Small example:

try:
    myfunc()
except ConnectionResetError or ConnectionAbortedError:
    print("foo")
except Exception as e:
    print("bar")

When myfunc() throws 'ConnectionAbortedError' the interpreter enters "bar" block, and not "foo" block.
msg401491 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-09 16:10
Expression `A or B` gets you A if A is true.

If you want to catch several types of exception by one "except", use except with a tuple:

    except (ConnectionResetError, ConnectionAbortedError):
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89316
2021-09-09 16:10:33serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg401491

resolution: not a bug
stage: resolved
2021-09-09 14:35:50ShlomiRexcreate