On Tue, Jan 4, 2022 at 3:32 PM elvis kahoro <el...@warp.dev> wrote: >> >> Reading between the lines, I *think* that you want the match statement >> to catch the exception that you get when the attribute lookup fails, am >> I right? > > Yes! > > I was hoping there could be some syntax to extend pattern matching to handle > exceptions such that we could handle patterns with multiple types of > exceptions like so: > > match this_raises_an_exception, this_raises_another_exception: > case AttributeError, TypeError: > print("catches attribute and type errors") > case AttributeError, AttributeError: > print("catches attribute and attribute") > case Exception, Exception: > print("catches the remaining exceptions") > case x, y: > print(f"{x} and {y}") > case _, _: > print("everything else") > > Any thoughts on this kind of syntax? > Maybe the author could explicitly distinguish that an exception might be > raised by using with Exception like so: > > match this_raises_an_exception, this_raises_another_exception with Exception: >
Question: Why? What's wrong with the existing syntax? Just because match/case syntax exists, that doesn't mean it has to be used for everything that selects different options. Exceptions are almost exclusively matched by type, nothing else, and we already have a very good syntax for doing that. The only situation where you'd want any other sort of exception matching is when it's a single type that carries multiple causes of error: try: ... except OSError[errno.EEXIST]: ... But as you can see here, this could easily be accomplished using the existing syntax, if we needed a way to do it (the use-cases are uncommon, given that we have eg FileNotFoundError). When, in production code, have you *ever* needed to match on two different exceptions at once? ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/FRQTSO6HEL4MHDQTCZ7SMSY2L7VJGYRV/ Code of Conduct: http://python.org/psf/codeofconduct/