0

Zero o type Error ?

try: variable = 10 print (variable + "hello") print (variable / 0) except ZeroDivisionError: print ("Divided by zero") except (ValueError, TypeError): print ("An error occurred") I don't understand, with this division value, shouldn't ZeroDivisionError skip? but instead I get the error occurred

12th Sep 2020, 4:10 PM
Gerardo
Gerardo - avatar
7 Respostas
+ 4
The following code does catch all exceptions and ahows the error message: try: variable = 10 print (variable + "hello") print (variable / 0) except Exception as e: print('the following error occurred: ',e)
12th Sep 2020, 5:58 PM
Lothar
Lothar - avatar
+ 3
Gerardo , should your code be python ?
12th Sep 2020, 4:21 PM
Lothar
Lothar - avatar
+ 2
In programming, we can't divide any number with zero. If you do you will get the error and in python, this error called ZeroDivisionError. but in the second line, you are adding string with an integer which is TypeError that's why you getting an error occurred.
12th Sep 2020, 4:15 PM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Because the error is rising due to line 2 ( print(variable + 'hello') ) as you are trying to add variables of different datatype thus generating value error.
12th Sep 2020, 4:15 PM
Arsenic
Arsenic - avatar
+ 2
Thank all
19th Nov 2020, 3:12 AM
Gerardo
Gerardo - avatar
+ 1
TypeError happens first so it has to handle it first.
12th Sep 2020, 4:17 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
First exception is in print( variable + "hello") type error..
12th Sep 2020, 4:29 PM
Jayakrishna 🇮🇳