Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching Runtime Errors

Status
Not open for further replies.

mcindafizzy

Programmer
Joined
Jan 7, 2005
Messages
1
Location
US
I am a rather new programmer and I am currently working on a database driven quiz/game application. I have a need to catch the Runtime error that occurs when a the program tries to open a file that doesn't exist. Unfortanetly I just can't find any documentation on how to catch runtime errors, and therefore I am here. Thanks
 
You need to switch off IO checking when you do your reset then check IOResult.

Like:

assign(file1, 'xxxx.dat');
{$I-}
reset(file1);
{$I+}
if IOResult <> 0 then
begin
writeln('I can not find my data file, terminating!');
halt(10);
end;
 
note that (so far as I know) IOresult is reset to zero when you check it (similar behaviour to doserror), so if you need to check it twice, you have to assign its value to a variable and check the variable twice.
 

A simpler solution in some compilers (such as FPC for example) is the FileExists function, this works like this;

IF FileExists(myfile) THEN carry on ELSE its an error

As far as I know this is actually the same code as Glenn's example but in a ready built function.
 
if u are useing 7.0 the fsearch function is useful so a function like this would work, needs the dos unit

uses dos;
function fileexist(filename : string):boolean;
var
s : pathstr;
begin
s := fsearch(filename,getenv('PATH'));
if s = '' then
fileexist := false
else
fileexist := true;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top