+ 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
4 ответов
+ 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
+ 1
Execution of statements stopped after there was error for 20/0
+ 1
Because you can't divide by zero.
So at that point, execution jumps into the except block.
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