语句预期但函数在我的函数代码中发现错误

Statement expected but Function found error in my function code

所以下面是我创建的代码模块,用于验证用户输入是否在一个范围内,但是这个错误一直出现在它下面“期望语句但找到函数”,任何人有什么想法吗?非常感谢

function get_choice(var Options: integer): integer;
      var
      choice: integer;

      begin
        while (true) do
          begin
              write('choose option 1 to', Options);

              try
                readln(choice);
                if (choice>=1) and (choice <=Options) then
                    get_choice := choice
                else
                    write('invalid range');
              except
                 write('not a number');
          end;
      end;

您错过了 end:

              try
                readln(choice);
                if (choice>=1) and (choice <=Options) then
                    get_choice := choice
                else
                    write('invalid range');
              except
                 write('not a number');
              end;
         end
      end;

尝试try..except块必须有自己的结束。

正如 Sharam 所说,您错过了 try..except..end 块的 end。不过,我想我可以通过一些技巧来增强答案,以帮助您将来避免这种情况。

  • 将您的 (begin .. end 块放在同一列。对于 try..except..endtry..finally..endrepeat..until 块也是如此。
  • 利用 IDE 的选项用线连接开始和块。 "结构高亮。查看附件截图。