这段代码中Finally 块中的语句是否仍然执行?

发布于 2024-12-22 09:04:37 字数 149 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

歌枕肩 2024-12-29 09:04:37

是的,finally 块始终会执行,即使您在某处调用 Exit 也是如此。如果它们不总是被执行,它们就没有多大价值。

Yes, finally blocks always execute, even if you call Exit somewhere. They wouldn't be worth much if they weren't always executed.

哑剧 2024-12-29 09:04:37

finally 子句将始终被执行,除非执行线程在执行 try 子句时进入非终止循环、无限期阻塞或异常终止。

相关文档指出(强调我的):

try...finally 语句的语法是

<前><代码>尝试
语句列表1
最后
语句列表2
结尾

其中每个statementList是一个语句序列
以分号分隔。

try...finally 语句执行
statementsList1 中的语句(try 子句)。如果statementList1完成时没有引发异常,
执行statementList2(finally 子句)。如果在执行statementList1期间引发异常,
控制权转移到statementList2;一次语句List2
执行完毕,异常会重新引发。 如果致电
退出、中断或继续过程导致控制权离开
自动执行statementList1、statementList2。

因此,无论 try 子句如何终止,finally 子句始终都会执行。

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):

The syntax of a try...finally statement is

try 
  statementList1
finally
  statementList2 
end 

where each statementList is a sequence of statements
delimited by semicolons.

The try...finally statement executes
the statements in statementList1 (the try clause). If statementList1 finishes without raising exceptions,
statementList2 (the finally clause) is executed. If an exception is raised during execution of statementList1,
control is transferred to statementList2; once statementList2
finishes executing, the exception is re-raised. If a call to the
Exit, Break, or Continue procedure causes control to leave
statementList1, statementList2 is automatically executed.

Thus the finally clause is always executed, regardless of how the try clause terminates.

同展鸳鸯锦 2024-12-29 09:04:37

一个快速测试应用程序可以很快回答这个问题。

program TestFinally;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  try
    WriteLn('Before exiting');
    Exit;
  finally
    WriteLine('In finally. If you see this, it was written after "Exit" was called');
    ReadLn;
  end;
end.

A quick test app could have answered this question really quickly.

program TestFinally;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  try
    WriteLn('Before exiting');
    Exit;
  finally
    WriteLine('In finally. If you see this, it was written after "Exit" was called');
    ReadLn;
  end;
end.
何以心动 2024-12-29 09:04:37

为了完整起见 - 如果执行 try..finally 块的进程或线程被 TerminateProcess/TerminateThread 终止,finally 块将不会执行。

例如,下面代码中的finally块将不会被执行。

o := TObject.Create;
try
  TerminateThread(GetCurrentThread, 0);
finally
  o.Free;
end;

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.

o := TObject.Create;
try
  TerminateThread(GetCurrentThread, 0);
finally
  o.Free;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文