einouterror i o error 105

fpm_start( "true" );

var
Form2: TForm2;
x: Integer;
y: Integer;
rezult: Integer;
count: Integer;
F: TextFile;
Pos: Integer;
TimeCount: Integer;

procedure TForm2.FormCreate(Sender: TObject);
begin
randomize;
x := random(11);
y := random(11);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
if Edit1.Text = » then ShowMessage(‘Ââåäèòå ÷èñëî!’);
If StrToInt(Edit1.Text) = rezult then
begin
ShowMessage(‘Ïðàâèëüíî!’);
count := count+1
end
else ShowMessage(‘Íåïðàâèëüíî!’);
Shape1.Top:=0;
if count = 1 then
begin
x := random(11);
y := random(11);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
end;
if count = 2 then
begin
x := random(21);
y := random(21);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=900
end;
if count = 3 then
begin
x := random(31);
y := random(31);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=800

if count = 4 then
begin
x := random(41);
y := random(41);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=700
end;
if count = 5 then
begin
x := random(51);
y := random(51);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=600
end;
if count = 6 then
begin
x := random(61);
y := random(61);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=500
end;
if count = 7 then
begin
x := random(71);
y := random(71);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=400
end;
if count = 8 then
begin
x := random(81);
y := random(81);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=300
end;
if count = 9 then
begin
x := random(91);
y := random(91);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=200
end;
if count = 10 then
begin
x := random(101);
y := random(101);
rezult := x+y;
Label1.Caption:=IntToStr(x);
Label3.Caption:=IntToStr(y);
Timer1.Interval:=100
end;
if count = 11 then
begin
ShowMessage(‘Âû âûèãðàëè!’);
count:=0;
Timer1.Enabled:=False;
Form2.Close;
end;
end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
Shape1.Top := Shape1.Top + 1;
if Shape1.Top = Form2.ClientHeight-50 then
begin
ShowMessage(‘Game Over!’);
count:=0;
Shape1.Top:=0;
Form2.Close;
end;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AssignFile(F, ‘StatTime.dat’);
<$I->
Timer

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AssignFile(F, ‘StatTime.dat’);
<$I->
Timer2.Enabled:=False;
Rewrite(F);
Reset(F);
<$I+>
Pos:=0;
Write(F,TimeCount);
CloseFile(F);
end;

procedure TForm2.Timer2Timer(Sender: TObject);
begin
TimeCount:=TimeCount+1
end;

I’m using old style Pascal I/O routines and expect that calls to I/O functions that fail should raise an EInOutError . When I try this I do not see an exception raised and I have no clue why.

2 Answers 2

The exception EInOutError will only be raised if I/O checking is enabled. To make sure it is enabled do the following:

  • In your project options, in «Delphi Compiler Options» check the checkbox I/O checking
  • Remove any <$IOCHECKS OFF>or <$I->directives from your code as they disable I/O checking

This should give you a proper exception if the file doesn’t exist.

Now if (for whatever reason) you cannot enable I/O checking:

With disabled I/O checking you won’t get an EInOutError if something goes wrong. Instead you have to check the value of IOResult after every I/O operation. It’s like in old Pascal times: If IOResult <> 0 then an error happened. This (slightly adapted) excerpt from the Delphi docs shows how to work with IOResult:

However, nowadays you should use TFileStream to access/create files and don’t use the old style Pascal routines anymore. An example how this could look:

I interpret your question that you would like EInOutError exceptions to be raised whenever a Pascal style I/O function fails. In order to do this you need to enable the I/O checking compiler option.

I/O checking: Enables or disables the automatic code generation that checks the result of a call to an I/O procedure. If an I/O procedure returns a nonzero I/O result when this switch is on, an EInOutError exception is raised (or the program is terminated if exception handling is not enabled). When this switch is off, you must check for I/O errors by calling IOResult.

I guess the code you are working with was written under the assumption that the I/O checking option was enabled, but that you are compiling with it not enabled. Here’s a bit of code that demonstrates EInOutError being raised due to an I/O error.

I strongly recommend that you enable I/O checking. This will allow you to handle errors using exceptions in a manner consistent with the rest of your code.

Not using I/O checking forces you to check the value of IOResult after every I/O function. This is very error prone (it’s easy to forget to check) and results in untidy code.

If you are already running with I/O checking enabled then the most likely explanation for you not seeing an error is that in fact no error is occurring. Perhaps the file does in fact exist.

Оцените статью