Why the output of this code not include print(2) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why the output of this code not include print(2)

try: ... print(1) ... print(20/0) ... print(2) ... except ZeroDivisionError: ... print(3) ... finally: ... print(4) 1 3 4

4th May 2020, 4:24 PM
Guillermo González
4 Answers
+ 2
print(20/0) print(2) You are using 'print(20/0)' above 'print(2)'. So when 20 is devided by 0, 'except' catches it and hence there is no scope left for 2 to be printed. So, it prints whatever inside the ZeroDivisionError or it just prints 3. Then finally it prints 4! So the output you get is : 1 3 4 However, if you write it like this : print(2) print(20/0) Then you'll get this :)) 1 2 3 4 https://code.sololearn.com/cb24nsAbAhUm/?ref=app
4th May 2020, 4:28 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 1
Execution of statements stopped after there was error for 20/0
4th May 2020, 4:28 PM
Abhay
Abhay - avatar
+ 1
Because you can't divide by zero. So at that point, execution jumps into the except block.
4th May 2020, 4:28 PM
HonFu
HonFu - avatar
0
Because after line 3 (where a error occur) I program stop and go to see exception which is ZeroDivisionError of line 3 And print code under it which print 3 And code under finally execute at end of program always which print 4
4th May 2020, 4:29 PM
Abhay
Abhay - avatar