现在的位置: 首页 > 综合 > 正文

异常处理&创建日志文件

2013年12月10日 ⁄ 综合 ⁄ 共 536字 ⁄ 字号 评论关闭
procedure cLogFile(txt: string);
var
  Tf: Text;
  sFile: string;
begin
  sFile := 'Recod.Log';
  try
    try
      AssignFile(tF, sFile);
      if not FileExists(sFile) then
        Rewrite(tF)
      else
        Append(tF);
      Writeln(tf, txt);
    except
      on EInOutError do                               //捕捉异常
        ShowMessage('写入错误');
    end;
  finally
    CloseFile(tF);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  cLogFile('日志时间:' + (DateTimeToStr(now)));
  for i := 0 to 10 do
    cLogFile('设置变量i:=' + IntToStr(i));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  try
    StrToInt('aa');
  except
    on e: Exception do
      ShowMessage(E.ClassName + E.Message);
  end;
end;

抱歉!评论已关闭.