0

Why does a code where many exceptions happens at the same time only execute one "except"?

In this code two exceptions happen at the same time (zero division and name error) try: print(variable) print(10/0) except ZeroDivisionError: print("Divided by zero") except NameError: print("Error occurred") Output: Error occurred Why?? What about Zero division error? Does python have a preference list of exceptions, and if the first happens it ignores the others? Thanks!!

16th Sep 2016, 7:00 PM
Miguel Díaz
Miguel Díaz - avatar
2 odpowiedzi
+ 6
the try statement stops running as soon at it hits an error. so with your code it reads the first print and immediately goes to the except NameError block. If you change the order of the print statements it should give you the other error.
16th Sep 2016, 7:55 PM
Luke Armstrong
0
Aaaah okeyy! Thank you very much!!
16th Sep 2016, 7:57 PM
Miguel Díaz
Miguel Díaz - avatar