1
def test_exception(num):
print(1)
try:
print(2)
answer = 100 / num
return answer
print(3)
except ZeroDivisionError as e:
print(4)
raise e
print(5)
print('start')
try:
test_exception(1)
print(6)
except ZeroDivisionError as e:
print(7)
raise e
finally:
print('end')
Execution result of 1
start
1
2
6
end
print ('start') Run first.
next, Execute a try block. Enter the test_exception function on the first line of the try block with argument 1. So Execute print (1) and Execute in the try block in the test_exception function. Execute print (2) and answer = 100 And returns 100. Because it is returning Note that print (3) below it is not executed. Since ZeroDivisionError has not occurred The except block in the test_exception function is not executed and is skipped.
Now that the test_exception function of the try block is finished Then execute print (6).
Since ZeroDivisionError has not occurred The except block is not executed and is skipped.
Finally, execute the finally block.
If the argument of the test_exception function is 0,
2
def test_exception(num):
print(1)
try:
print(2)
answer = 100 / num
return answer
print(3)
except ZeroDivisionError as e:
print(4)
raise e
print(5)
print('start')
try:
test_exception(0)
print(6)
except ZeroDivisionError as e:
print(7)
raise e
finally:
print('end')
Execution result of 2
start
1
2
4
7
end
Recommended Posts