I am playing with this new match-case syntax in Python 3.10 and I had a thought whether one would be able to catch exceptions with it. Is it possible?
Let’s take a look!
>>> def say(something): ... match something: ... case "hello": ... print("Hello to you!") ... case TypeError: ... print("Uhm, what was that you said?!") ... >>> say(123) Uhm, what was that you said?! >>> say("hello") Hello to you!
As you can see, you definitely can! Isn’t Python just amazing? Of course your answer is a resounding yes.