Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Except multiple types of exceptions with OR keyword is not working #89316

Closed
ShlomiRex mannequin opened this issue Sep 9, 2021 · 2 comments
Closed

Except multiple types of exceptions with OR keyword is not working #89316

ShlomiRex mannequin opened this issue Sep 9, 2021 · 2 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ShlomiRex
Copy link
Mannequin

ShlomiRex mannequin commented Sep 9, 2021

BPO 45153
Nosy @serhiy-storchaka, @ShlomiRex

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-09-09.16:10:33.161>
created_at = <Date 2021-09-09.14:35:50.101>
labels = ['interpreter-core', 'type-bug', 'invalid', '3.9']
title = 'Except multiple types of exceptions with OR keyword is not working'
updated_at = <Date 2021-09-09.16:10:33.158>
user = 'https://github.com/ShlomiRex'

bugs.python.org fields:

activity = <Date 2021-09-09.16:10:33.158>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2021-09-09.16:10:33.161>
closer = 'serhiy.storchaka'
components = ['Interpreter Core']
creation = <Date 2021-09-09.14:35:50.101>
creator = 'ShlomiRex'
dependencies = []
files = []
hgrepos = []
issue_num = 45153
keywords = []
message_count = 2.0
messages = ['401486', '401491']
nosy_count = 2.0
nosy_names = ['serhiy.storchaka', 'ShlomiRex']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45153'
versions = ['Python 3.9']

@ShlomiRex
Copy link
Mannequin Author

ShlomiRex mannequin commented Sep 9, 2021

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.

@ShlomiRex ShlomiRex mannequin added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 9, 2021
@serhiy-storchaka
Copy link
Member

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):

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant