Re: Can't understand why these exceptions occur...



Jamie <jamie_ka1lpa_not_valid_after_ka1lpa_@xxxxxxxxxxx> wrote in
news:Kki0k.41$sf2.13@xxxxxxxxxxxx:

<snip>
What compiler are you using?
I Hope not .NET!

No, I'm using Turbo Delphi (Borland Developer Studio 2006 to be precise).

also,
You didn't read carefully as to what I said about
IoResult.
It clears the state after each read.
this means.
If you do this
If Ioresult <> 0 THen WriteLn(IoResult), it'll always
be 0~! becayse the IoResult is a function and clears the internal
state before returning and returns what the state was..
If you access it again, like in your writeln, it will also be
0 because it's been cleared.!

Ah, indeed I misread that. Sorry. But in retrospect, it would not have
changed anything - if the first call to IOResult is not 0, then the
second call would indeed be 0 but it would still be written to the
screen. Since nothing was written to the screen, I assume it's safe to
say that IOResult was always 0.

Anyway, I've changed the code again to include your suggestions.

<snip>
Or Do this , write a log Error Log Function...
Procedure IoErrorLog(Var LineNUmber:Integer);
Var
InoutRes:Integer;
begin
InoutRes := IoResult;
If INoutRes <> 0 then
WriteLn('IO error at Line #:',LineNumber');
End;

then you simple do this in your code

IoErrorLog(LineNumber?);

I preferred this way - it's easier to add and clearer to view in code,
IMHO. Here are my changes:

--- LoadCache ---
procedure LoadCache(Const cacheName : String);
var
cache : File of TBufferInfo;
begin
try
if FileExists(cacheName) then
begin
AssignFile(cache, cacheName);
IoErrorLog(148);
Reset(cache);
IoErrorLog(150);
idx := 1;
while Not(eof(cache)) and (idx < Length(files)) do
begin
Read(cache, files[idx]);
IoErrorLog(155);
Inc(idx);
end;
CloseFile(cache);
IoErrorLog(159);
end;
except on e : Exception do
WriteLn('Exception occured: ' + e.Message);
end;
Exit;
end;

--- SaveCache ---
procedure SaveCache(Const cacheName : String);
var
cache : File of TBufferInfo;
begin
try
AssignFile(cache, cacheName);
IoErrorLog(177);
ReWrite(cache);
IoErrorLog(179);
idx := 1;
while (idx < Length(files)) do
begin
if (files[idx].name <> '') then
begin
Write(cache, files[idx]);
IoErrorLog(186);
end;
Inc(idx);
end;
CloseFile(cache);
IoErrorLog(191);
except on e : Exception do
WriteLn('Exception occured: ' + e.Message);
end;
end;

--- Added IOErrorLog ---
procedure IoErrorLog(LineNumber : Integer);
var
InoutRes:Integer;
begin
InoutRes := IoResult;
if INoutRes <> 0 then
begin
WriteLn('IO error at Line #:', LineNumber);
end;
end;

--- Changed main ---
IoErrorLog(251);
LoadCache('FileLocatorBuffer.dat');
IoErrorLog(253);

if LowerCase(ParamStr(1)) = 'buffer' then
begin
IoErrorLog(257);
StartBufferingFiles(ParamStr(2));
IoErrorLog(259);
end;

IoErrorLog(269);
SaveCache('FileLocatorBuffer.dat');
IoErrorLog(271);
---

After compiling and running the program, here is what is written in the
console:
--- output ---
C:\Borland Studio Projects\FileLocator>FileLocator buffer c:\temp
Exception EInOutError in module FileLocator.exe at 00007E85.
I/O error 103.

Exception EAccessViolation in module FileLocator.exe at 000057C4.
Access violation at address 004057C4 in module 'FileLocator.exe'. Read of
addres
s 10884CF0.

Exception EAccessViolation in module FileLocator.exe at 000057C4.
Access violation at address 004057C4 in module 'FileLocator.exe'. Read of
addres
s 10884CF0.
---

As you can see, IoErrorLog never does the WriteLn, so IoResult is always
0 if I understand it correctly.

Still, the errors keep popping up...

Just remember , your Pascal file code will not function correctly
if there is a lingering error.

I know - I just wish I could find said lingering error ;)

Thanks, Jamie, I really appreciate your efforts!

Ikke
.



Relevant Pages