这里是代码
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")
在输入中,我给出了一个字符串(例如:wefervrsvr),因此它必须经过具有ValueError
功能的except块中的print
,然后通过continue
语句使程序控制为循环的顶部,所以它再次请求我们输入,但是我在这里不明白的是,为什么最终要在控件跳转到顶部尝试阻塞之前执行,就像我在输出中看到的那样。
on the way out
块的存在是为了确保您可以执行一些代码,无论finally
块中发生了什么。 try
关键字不会规避它,即使未处理的异常也不会规避它。