zoukankan      html  css  js  c++  java
  • 捕获ClientDataSet.ApplyUpdates和SocketConnection异常

    核心提示:如何捕获ClientDataSet.ApplyUpdates的错误,不用ReconcileError...
    var
      cdsEmp:TClientDataSet;
    //保存
    procedure  TfrmEmp.btnSave(Sender:  TObject);
    begin
      cdsEmp.RemoteServer.AppServer.BegTrans;
      try
        cdsEmp.ApplyUpdates(0);  //更新错误在这一句发生,但是我却永远也捕获不到,
                                 //我想自已在异常处理里显示这里发生的错误信息该怎么办?
        cdsQrObj.RemoteServer.AppServer.ComTrans;
      except
        on  E:Exception  do
        begin
          cdsEmp.RemoteServer.AppServer.RobTrans;
          Application.MessageBox(pchar('存盘失败!'+#13#10+'错误信息:'+E.Message),'提示',MB_OK+MB_ICONEXCLAMATION);
          Abort;
        end;
      end;
    end;
    //如果用这个错误处理,我的事务回滚却不知放在何处才妙,并且我不是想用这个错误处理
    procedure  TfrmEmp.cdsEmpReconcileError(
       DataSet:  TCustomClientDataSet;  E:  EReconcileError;
       UpdateKind:  TUpdateKind;  var  Action:  TReconcileAction);
    begin
       HandleReconcileError(DataSet,  UpdateKind,  E);
       Action:=raAbort;
    end;
    解答一:
    //---------------------------------------------------------------
    //据我所知,只能用ReconcileError 可以用下面的方法判断是否错误
    ...
    BeginTransaction;
    if cdsMaster.ApplyUpdates(0)+cdsDetail.ApplyUpdates(0)=0  then
      CommitTransaction
    else
      RollbackTransaction;
    ApplyUpdates方法返回更新时遇到的错误数量.
    ...
    //---------------------------------------------------------------
     
    解答二:
    //---------------------------------------------------------------
    在DataSetProvider的onUpdateError
    raise  E;
    然后就可以在客户端的
    try
      ClientDataSet1.ApplyUpdates(0);
    except
      on  e:Exception  do
      ...
    end;
    //----------------------------------------------------------------
    解答三:
        其实真正的捕获ClientDataSet.ApplyUpdate异常的方法应该是在Apllication的异常中捕获并处理它。因为ClientDataSet抛出的异常为线程(进程?)异常,在ClientDataSet的ApplyUpdate中用try...except...end;是无法捕获的。
        具体方法为:在公共单元如DataModule中放置一个ApplicationEvent件,在该控件的OnException事件中捕获异常,该窗体应在所有有可能产生ApplyUpdate或Connection异常的窗体之前创建。
    procedure TClient_RDataForm.ApplicationEvents1Exception(Sender: TObject;
      E: Exception);
    begin
      if (E is ESocketConnectionError) or (E is ESocketError) then
      begin
        if not Is_OK then
        begin
          Application.MessageBox(PChar('考试应用服务器或网络连接失败!请退出后重新启动考试系统!  '),
                          '服务器连接中断', MB_OK + MB_ICONERROR);
          Application.Terminate;
          Exit;
        end;
        while not ReConnect_Srv do //重新连接又失败了
        begin
          if Application.MessageBox(PChar('考试服务器或网络连接失败!请立即与监考老师联系!  '+#13+'要重新搜索服务器请按[是],强制退出请按[否]!  '),
                          '服务器连接失败', MB_YESNO + MB_ICONSTOP) <> IDYES then
            if (Application.MessageBox('真的要强制退出考试系统吗?  ','强制退出确认', MB_YESNO + MB_ICONWARNING + MB_DEFBUTTON2) = IDYES) then
            begin
               Application.Terminate;
               Exit;
            end;
        end;
        if MyConnection.Connected then
        begin
          Application.MessageBox('考试应用服务器恢复连接成功!','连接成功',MB_OK+MB_ICONINFORMATION);
          Exit;
        end;
      end else
        raise Exception.Create('考试系统发生异常错误!退出后请重新启动考试系统继续考试!');
        //ShowMessage(e.Message);
    end;
    




  • 相关阅读:
    JavaScript模态对话框类
    事件模块的演变(1)
    html5中可通过document.head获取head元素
    How to search for just a specific file type in Visual Studio code?
    What do 'lazy' and 'greedy' mean in the context of regular expressions?
    正则非获取匹配 Lookahead and Lookbehind ZeroLength Assertions
    regex length 正则长度问题
    Inversion of Control vs Dependency Injection
    How to return View with QueryString in ASP.NET MVC 2?
    今天才发现Google Reader
  • 原文地址:https://www.cnblogs.com/xieyunc/p/9126571.html
Copyright © 2011-2022 走看看