这段代码中Finally 块中的语句是否仍然执行?
finally 会阻塞执行吗?如果我通过出口; ?
procedure someProc;
begin
Try
Exit;
finally
do_something;
end;
end;
Will finally block execute? if I pass exit; ?
procedure someProc;
begin
Try
Exit;
finally
do_something;
end;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,
finally
块始终会执行,即使您在某处调用Exit
也是如此。如果它们不总是被执行,它们就没有多大价值。Yes,
finally
blocks always execute, even if you callExit
somewhere. They wouldn't be worth much if they weren't always executed.finally 子句将始终被执行,除非执行线程在执行 try 子句时进入非终止循环、无限期阻塞或异常终止。
相关文档指出(强调我的):
The finally clause will always be executed, unless the executing thread enters a non-terminating loop, blocks indefinitely or is terminated abnormally, whilst executing the try clause.
The pertinent documentation states (emphasis mine):
一个快速测试应用程序可以很快回答这个问题。
A quick test app could have answered this question really quickly.
为了完整起见 - 如果执行 try..finally 块的进程或线程被 TerminateProcess/TerminateThread 终止,finally 块将不会执行。
例如,下面代码中的finally块将不会被执行。
For the sake of completeness - finally block will not execute if the process or thread executing the try..finally block is terminated with TerminateProcess/TerminateThread.
For example, finally block will not be executed in the code below.