0

In a try-except if u have more than 1 except, which 1 is going to be executed? The first exception it will catch or all of them?

try: print(1) assert 2+2==5 except AssertionError: print(3) except: print(4) # Is not (except:) stronger than (except EXCEPTION:)

4th Dec 2016, 3:42 PM
Nektarios Marmaras
Nektarios Marmaras - avatar
4 Réponses
+ 6
If there's an exception, and it matches the exception classes of several "except" clauses, only the first one will be executed. In the example, you'll get "1" and "3" as the output. EDIT: regarding the comment "is not (except:) stronger..." -> in fact, it means the default case, when there's an exception not caught by any former "except..." clause. So "except AssertionError" is executed and afterwards the control passes to the sentence below the whole "try" block (if exists).
4th Dec 2016, 6:15 PM
Álvaro
+ 4
3
12th Jan 2018, 12:52 PM
CHANDRAN S
CHANDRAN S - avatar
+ 1
Thanks a lot. I get it now :)
4th Dec 2016, 8:26 PM
Nektarios Marmaras
Nektarios Marmaras - avatar
0
3 is the answer
10th Apr 2021, 2:49 AM
Grecelda Geverola
Grecelda Geverola - avatar