Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么尽管我们在EXPECTION块中有continue,但最终还是执行,该块将控制权转移到while的顶部?_Python_Exception_Try Catch Finally - Fatal编程技术网

Python 为什么尽管我们在EXPECTION块中有continue,但最终还是执行,该块将控制权转移到while的顶部?

Python 为什么尽管我们在EXPECTION块中有continue,但最终还是执行,该块将控制权转移到while的顶部?,python,exception,try-catch-finally,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

这是密码

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")
在输入中,对于age,我给出了一个字符串,例如:wefervrsvr,因此它必须通过具有打印功能的except块中的VALUETERROR,然后通过continue语句,使程序控制在循环的顶部,以便它再次向我们请求输入,但我不明白的是,为什么在控件跳转到顶部的try块之前最终执行,正如我在输出中看到的那样

来自:

当在try…finally语句的try套件中执行return、break或continue语句时,finally子句也会“在退出时”执行

“on the way out”基本上是指,如果在exception子句内执行continue语句,则将执行finally子句中的代码,然后循环将继续进行下一次迭代。

来自:

当在try…finally语句的try套件中执行return、break或continue语句时,finally子句也会“在退出时”执行

“on the way out”基本上是指,如果在exception子句中执行continue语句,则将执行finally子句中的代码,然后循环将继续进行下一次迭代。

finally块的存在是为了保证您可以执行某些代码,而不管try块中发生了什么。continue关键字不会绕过它,即使是未处理的异常也不会绕过它

例如,如果您删除了ValueError的捕获,您仍然会点击finally块:

try:
    raise ValueError("unhandled exception type");
except ZeroDivisionError:
    print("problems.")
finally:
    print("will print, even in the face of an unhandled exception")
finally块的存在是为了保证您可以执行某些代码,而不管try块中发生了什么。continue关键字不会绕过它,即使是未处理的异常也不会绕过它

例如,如果您删除了ValueError的捕获,您仍然会点击finally块:

try:
    raise ValueError("unhandled exception type");
except ZeroDivisionError:
    print("problems.")
finally:
    print("will print, even in the face of an unhandled exception")

这是一个不错的答案

import time;
while True:
    try:
        print("waiting for 10 seconds...\n")
        continue
        print("never show this")
    finally:
        print("Finally starts executing\n");
        time.sleep(10)
        print("\nFinally ends executing control jumps to start of the loop");

这是一个不错的答案

import time;
while True:
    try:
        print("waiting for 10 seconds...\n")
        continue
        print("never show this")
    finally:
        print("Finally starts executing\n");
        time.sleep(10)
        print("\nFinally ends executing control jumps to start of the loop");

这就是最终的目的,不管发生什么,它都会被执行。您甚至可以在try或except中有一个return语句,finally仍将被执行。但是为什么在continue的控件跳转到try块之前执行,为什么不在try块成功执行之后执行,然后else块跳出无限循环并最终在跳出之前执行。在continue语句中,python认识到,您将把try段跳转到while循环的开头,并最终执行finallyblock@Maestro因为它必须在整个尝试之前执行,除非最终退出,当它跳转到下一个循环迭代时就会退出。@schwobasegg我得到了它,多亏了很多这就是最终的意义,不管发生什么,它都会被执行。您甚至可以在try或except中有一个return语句,finally仍将被执行。但是为什么在continue的控件跳转到try块之前执行,为什么不在try块成功执行之后执行,然后else块跳出无限循环并最终在跳出之前执行。在continue语句中,python认识到,您将把try段跳转到while循环的开头,并最终执行finallyblock@Maestro因为它必须在整个尝试之前执行,除非最终退出,当它跳转到下一个循环迭代时就会退出。@schwobasegg我得到了它,非常感谢