简体   繁体   English

最终会在没有 python 的尝试块的情况下阻止工作吗?

[英]will finally block work without try block in python?

If I do not specify any try and except block in python, will finally work in the program?如果我没有在 python 中指定任何 try 和 except 块,最终会在程序中工作吗?

or I can use finally only with a try block.或者我只能在 try 块中使用finally

if condition:
   print('something')
else:
   print('something else')
finally:
   print('I will always run')

No, it won't work outside a try block, because finally is part of the try statement, as evident from Python's grammar :不,它不会在try块之外工作,因为finallytry语句的一部分,从Python 的语法中可以看出:

try_stmt: ('try' ':' suite
           ((except_clause ':' suite)+
            ['else' ':' suite]
            ['finally' ':' suite] |
           'finally' ':' suite))

So the finally keyword can occur only within the try_stmt production, which captures try statements.所以finally关键字只能出现在try_stmt产生式中,它捕获try语句。

No, finally is only used with try .不, finally仅与try一起使用。 Its purpose is to run some code after the try and/or any except block, whether or not the code in the try block threw an exception.它的目的是在try和/或任何except块之后运行一些代码,无论try块中的代码是否抛出异常。 The finally block is executed immediately after the try or except block, not delayed until the end of the program. finally块在tryexcept块之后立即执行,不会延迟到程序结束。 See the official docs for a more thorough explanation.请参阅官方文档以获得更详尽的解释。

finally is useful if you need to clean up some resources before continuing.如果您需要在继续之前清理一些资源, finally很有用。 For example:例如:

f = open('myfile.txt')
try:
    f.write('blabla')  # This might raise an exception, for example if the disk is full
finally:
    f.close() # Close the file whether the write failed or not.

Without finally we'd have to repeat the cleanup statement, like this:如果没有finally我们将不得不重复清理语句,如下所示:

f = open('myfile.txt')
try:
    f.write('blabla')
except:
    f.close()
    raise
f.close()

In Python, it is almost always better to use a with block instead of try / finally .在 Python 中,使用with块而不是try / finally几乎总是更好。

Finally, in your example, you might as well write:最后,在您的示例中,您不妨编写:

if condition:
   print('something')
else:
   print('something else')
print('I will always run')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python:如果finally块引发异常,则从try块恢复异常 - python: recover exception from try block if finally block raises exception 为什么在python中的“try:”块中的return语句之后执行“finally:”块? - Why "finally:" block is executed after the return statement in a "try:" block in python? 从finally块python中的try块中访问变量 - Access variable in try block from finally block python try-finally 块:当 finally 块应该在 else 块之前运行时,为什么下面的代码可以工作? - try-finally block: why does the following code work when the finally block is supposed to run before the else block? Python *与*语句完全等同于尝试 - (除外) - finally块? - Is Python *with* statement exactly equivalent to a try - (except) - finally block? 使用finally子句的try块中不允许使用python 2.4的产生方法 - Workaround for python 2.4's yield not allowed in try block with finally clause Python 最终阻止未捕获错误 - Python finally block not catching errors 如何终止 python 进程并在 python try-except 语句中输入 finally 块? - How to terminate a python process and enter the finally block in python try-except statement? finally 总是在 try 块返回之前运行,那么为什么 finally 块中的更新不会影响 try 块返回的变量的值呢? - Finally always runs just before the return in try block, then why update in finally block not affect value of variable returned by try block? finally 块在 python 中 try 子句的 break、continue、return 语句之前执行 - finally block execute before the break,continue,return statement of try clause in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM