Board index » delphi » handling io errors when using loadfromfile

handling io errors when using loadfromfile

How can I intercept and handle error exceptions when using
loadfromfile   loading text into a memo component?  If there is a file
not found, I want to catch it and allow the user to create one, not to
generate an error.  Any ideas.
 

Re:handling io errors when using loadfromfile


procedure TForm1.Button1Click(Sender: TObject);
var
  FileName: string;
begin
  FileName := 'c:\NoSuchFile.txt';
  try
    ListBox1.Items.LoadFromFile(FileName);
  except
    on EFOpenError do
    if MessageDlg('The file ' + FileName + ' does not exist' + #13 +
    'Would you  like to like to create it?',mtConfirmation,[mbYes,mbNo],0)
    = mrYes then //do something
    else Exit;
  end;
end;

Charles Johnson

Other Threads