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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I/O Error 21

Status
Not open for further replies.

ukusa

Programmer
Oct 8, 2001
49
AU
Howdy all,

What is the best way to handle "Missing Media" - selecting CD drive or A: drive with no disk inserted - which gives an I/O Error 21. Do I code it on Change or Application.On exception? Any examples would be appreciated! Thanks for any help!

Allen
 
The usual way is to wrap code that might fail (eg because of missing media) in a try ... except ... end block.

For example:-

Code:
try
  stringList := TStringList.Create;
  try
    stringList.LoadFromFile ( 'a:test.txt' );
    //   your code to process stringList
  finally
    stringList.Free;
  end;
except
  on E:EFOpenError do
    ShowMessage ( E.Message );
end;

E.Message contains a text description of the problem. In this example it is "Cannot open file a:test.txt".

Note that "EFOpenError is raised when an application cannot open a specified file". So this exception occurs when the file is missing from the media as well as when the media itself is missing.

You can put the try ... except around a call to a procedure. This might make your code clearer. For example:

Code:
try
  ProcessTestFile;
except
  on E:EFOpenError do
    ShowMessage ( E.Message );
end;

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top