在 if/else 和 try/finally 块中使用分号

Use of semicolons with if/else and try/finally blocks

我发现这段代码在下面指出的地方使用分号和不使用分号都可以编译。

这里使用分号的正确方法是什么?

  try
    try
      if MyBoolean = True then
      begin
        DoSomething;
      end
      else
      begin
        DoSomethingElse;
      end <<<--- Semi colon here?
    except
      ;
    end <<<--- Semi colon here?
  finally
    ;
  end;

这是记录在案的行为:

Declarations and Statements (Delphi): Compound Statements

A compound statement is a sequence of other (simple or structured) statements to be executed in the order in which they are written. The compound statement is bracketed by the reserved words begin and end, and its constituent statements are separated by semicolons. For example:

begin
  Z := X;
  X := Y;
  X := Y;
end;

The last semicolon before end is optional. So this could have been written as:

begin
  Z := X;
  X := Y;
  Y := Z
end;

还有:

Delphi's Object Pascal Style Guide: Statements

Statements are one or more lines of code followed by a semicolon. Simple statements have one semicolon, while compound statements have more than one semicolon and therefore consist of multiple simple statements.

Here is a simple statement:

A := B;

If you need to wrap the simple statement, indent the second line two spaces in from the previous line. Here is a compound, or structured, statement:

begin
  B := C;
  A := B;
end;

Compound Statements always end with a semicolon, unless they immediately precede an end keyword, in which case the semicolon is optional but recommended by this style guide.

您示例中的“结束关键字”将包括 exceptfinally