delphi - 为什么 I/O 错误无法引发异常?

标签 delphi pascal

我正在使用旧式 Pascal I/O 例程,并期望对失败的 I/O 函数的调用应引发 EInOutError。当我尝试这样做时,我没有看到引发异常,而且我不知道为什么。

procedure TForm1.Button1Click(Sender: TObject);
//var i: integer;
begin
id:=(strtoint(Edit1.Text)-1)*4;

AssignFile(plik,'\klienci\'+linia_klient[id]+'.txt');
try
Reset(plik);
except
  on EInOutError do Rewrite(plik);
  end;
edit2.Text:=linia_klient[id+1];
edit3.Text:=linia_klient[id+2];
//ListBox1.Clear;
//ListBox1.Items.Add();
end;

整个代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Edit2: TEdit;
    Edit3: TEdit;
    ListBox1: TListBox;
    Label4: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  plik:TextFile;
  linia_klient,linia_video:array[0..20] of string;
  id:integer;

implementation

{$R *.dfm}
{$IOCHECKS ON}
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
Edit1.Text:='Witaj, Podaj ID klienta';
Label1.Caption:='ID';
AssignFile(plik,'klienci.txt');
Reset(plik);
i:=0;
While Not Eof(plik) do
  begin
    Readln(plik,linia_klient[i]);
    inc(i);
  end;

CloseFile(plik);
AssignFile(plik,'video.txt');
Reset(plik);
i:=0;
While Not Eof(plik) do
  begin
    Readln(plik,linia_video[i]);
    inc(i);
  end;

CloseFile(plik);
end;

procedure TForm1.Button1Click(Sender: TObject);
//var i: integer;
begin
id:=(strtoint(Edit1.Text)-1)*4;

AssignFile(plik,'\klienci\'+linia_klient[id]+'.txt');
try
Reset(plik);
except
  on EInOutError do Rewrite(plik);
  end;
edit2.Text:=linia_klient[id+1];
edit3.Text:=linia_klient[id+2];
//ListBox1.Clear;
//ListBox1.Items.Add();
end;

end.

最佳答案

异常EInOutError仅当启用 I/O 检查时才会引发。要确保其已启用,请执行以下操作:

  • 在项目选项的“Delphi 编译器选项”中选中I/O 检查复选框
  • 从代码中删除所有 {$IOCHECKS OFF} 或 {$I-} 指令,因为它们会禁用 I/O 检查

如果文件不存在,这应该会给你一个适当的异常。

现在,如果(无论出于何种原因)您无法启用 I/O 检查:

如果禁用 I/O 检查,您将不会得到 EInOutError如果出现问题。相反,您必须检查 IOResult 的值每次 I/O 操作后。就像在旧的帕斯卡时代:If IOResult <> 0然后发生了错误。 此(稍作修改)摘自 Delphi docs显示如何使用 IOResult:

  AssignFile(F, FileName);
  {$I-}
  Reset(F);
  {$I+}
  if IOResult = 0 then
  begin
    MessageDlg('File size in bytes: ' + IntToStr(FileSize(F)),
      mtInformation, [mbOk], 0);
    CloseFile(F);
  end
  else
    MessageDlg('File access error', mtWarning, [mbOk], 0);

但是,现在您应该使用 TFileStream 访问/创建文件并且不再使用旧式 Pascal 例程。一个示例:

filename := '\klienci\'+linia_klient[id]+'.txt';
if not FileExists(filename) then
  // "Create a file with the given name. If a file with the given name exists, open the file in write mode."
  fs := TFileStream.Create(filename, fmCreate) else
  // "Open the file to modify the current contents rather than replace them."
  fs := TFileStream.Create(filename, fmOpenReadWrite);  

关于delphi - 为什么 I/O 错误无法引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8474970/

相关文章:

delphi - 创建错误消息 Delphi 7

delphi - Windows Message lparam转换

Delphi 数组 : Variable Myvar might not been initialized

delphi - 为什么 Delphi IDE 还包含 .NET 代码而不仅仅是 Object Pascal

json - super 对象 - 元素名称中的空格

delphi - 为什么@运算符返回的地址与GetProcAddress函数返回的地址不同

delphi - 如果我通过在 pascal 中分配一个未初始化的数组来清除数组,会发生什么不好的事情吗?

xml - 在 Delphi 中访问部分 XML 文档

delphi - 如何创建一个数据感知的TCheckListBox?

java - Delphi 中的 "Word"是否与 Java 中的 "Char"等效?