小编Mae*_*tro的帖子

尽管我们在 except 块中将控制权转移到 while 的顶部,但为什么最终会执行?

这是代码

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 exception try-catch-finally

2
推荐指数
1
解决办法
1331
查看次数

标签 统计

exception ×1

python ×1

try-catch-finally ×1