+ 1

How can I make exception work?

I have tried to make the example of except in a code which divide 2 in 0, but python tells me that I have an invalid Syntax and highlights the "e" of the word "except". I don't now what I am doing wrong, I would thank you so much if you know and let me know. num1 = 2 num2 = 0 print(num1 + num2) print("Done") except ZeroDivisionError: print("You can not do this")

20th Feb 2020, 10:51 PM
Leonardo Diego
Leonardo Diego - avatar
3 Réponses
+ 3
you are not using division to get the error. and you forgot the (try) block . here's an example. num1 = 2 num2 = 0 try: print(num1 / num2) print("done") except ZeroDivisionError: print("You can not do this")
20th Feb 2020, 11:09 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 4
If you want an error to be caught, you must enclose the code with the possibility of encountering an error with a "try". Purely using except will not lead to the catching of an error.
21st Feb 2020, 12:14 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
Ohhh okok, about the "number 1" and "+" it was a mistake when I was writing the comment, I don't have it in the code. But I didn't know I must put "try". Thank you so much 👍
20th Feb 2020, 11:17 PM
Leonardo Diego
Leonardo Diego - avatar