有什么方法忽略报错信息呢?

解决方案 »

  1.   

    报什么错?用 异常处理
    try
      //do something
    exceptend;
      

  2.   

    给个通用的捕获异常的例子:procedure AppException(Sender: TObject; E: Exception); implementationprocedure TMainForm.AppException(Sender: TObject; E: Exception);
    var
      sMsg: string;
    begin
      sMsg := E.Message;
      WriteLog(sMsg);
      //StatusBar1.Panels[0].Text := (sMsg);
      //ShowMessage(sMsg);
    end;...procedure TMainForm.FormCreate(Sender: TObject);
    begin
      Application.OnException := AppException;  //添加在主窗体里面,整个程序raised的异常都被这里接收。
    end;...try 
      //do something 
    except 
      raised; //重新触发异常,被Application接收
    end; 
    //写日志的函数
    procedure TFun.WriteLog(sLog: string);
    var
      path_file: string;
      dtNow: TDateTime;
      F: TextFile;
    begin
      dtNow := Now;
      sLog := sType + FormatDateTime('[ yyyy-MM-dd hh:mm:ss ]' + #9, dtNow) + sLog;  path_file := ExtractFilePath(Application.ExeName) + 'log\';
      if not DirectoryExists(path_file) then
      begin
        ForceDirectories(path_file);
      end;
      path_file := path_file + FormatDateTime('yyyymmdd', dtNow) + '_log.log';  try
        AssignFile(F, path_file);
        if not FileExists(path_file) then
          Rewrite(F)
        else
          Append(F);
        Writeln(F, sLog);
        Flush(F);
        CloseFile(F);
      except
        Flush(F);
        CloseFile(F);
        raise;
      end;
    end;
      

  3.   

        try    except
          on e: Exception do
          begin
            errstr := '错误' + e.Message + ' strsql:' + strsql;
            WriteLog(errstr);
          end;
        end;procedure WriteLog(const str: string);
    var
      strLog: string;
      strFileName, strFilePath: string;
      hLogFile: integer;
    begin
      try
        if not bNeedLog then Exit;
        if not bOpened then //如果文件还没有打开就先打开
        begin
          strFilePath := ExtractFilePath(ParamStr(0)) + 'log';
          if not DirectoryExists(strFilePath) then MKDir(PChar(strFilePath));
          strFileName := strFilePath + '\' + DateToStr(date) + '.log';
          if not FileExists(strFileName) then
          begin
            //如果文件不存在就先创建
            hLogFile := FileCreate(strFileName);
            if hLogFile > 0 then FileClose(hLogFile)
            else Exit;
          end;
          AssignFile(logFile, strFileName);
          Append(logFile);
          bOpened := true;
        end;
        
        strLog := DatetimeToStr(now) + '---->' + str;
        Writeln(logFile, strLog);
        flush(logFile);
      except
      end;
    end;
      

  4.   

    错误报告是:Violation of PRIMARY KEY constrait 'PK_yfjb0'.Cannot insert duplicate key in object 'yfjb0'