为什么我们要继续执行,但是我们除了在块中将控件转移到while的顶部?

问题描述 投票:0回答:2

这里是代码

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语句使程序控制为循环的顶部,所以它再次请求我们输入,但是我在这里不明白的是,为什么最终要在控件跳转到顶部尝试阻塞之前执行,就像我在输出中看到的那样。

python exception try-catch-finally
2个回答
2
投票

1
投票

on the way out块的存在是为了确保您可以执行一些代码,无论finally块中发生了什么。 try关键字不会规避它,即使未处理的异常也不会规避它。

© www.soinside.com 2019 - 2024. All rights reserved.