您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

捕获ClientDataSet.ApplyUpdates和SocketConnection异常

时间:2011/9/3 15:32:03 点击:

  核心提示:varcdsEmp:TClientDataSet;//保存procedure TfrmEmp.btnSave(Sender: TObject);begin cdsEmp.RemoteServer.Ap...

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;

Tags:捕获 异常 
作者:小宇飞刀 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号