Mae*_*tro 2 python exception try-catch-finally
这是代码
while True:
try:
age = int(input("Enter your age"))
except ValueError:
print("Enter the age in integer")
continue
except ZeroDivisionError: #when trying to divide the age for an age groups
print("Age cannot be zero")
continue
else:
print("thank you!!")
break
finally:
print("ok! I am finally done")
Run Code Online (Sandbox Code Playgroud)
在输入中,对于年龄,我给出一个字符串(例如:wefervrsvr),因此它必须经过ValueErrorexcept 块中的 ,该块具有print函数和 thencontinue语句,该语句使程序的控制到达循环的顶部,因此它再次要求输入来自我们,但我在这里不明白的是,为什么最终会在控制跳转到顶部的 try 块之前执行,正如我在输出中看到的那样。
来自python docs:
\n\n\n当在 try...finally 语句的 try 套件中执行 return、break 或 continue 语句时,finally 子句也会在退出时执行 \xe2\x80\x98\'。
\n
“on the way out”基本上意味着,如果在continue异常子句内执行语句,finally则将执行子句中的代码,然后循环将继续进行下一次迭代。
| 归档时间: |
|
| 查看次数: |
1331 次 |
| 最近记录: |