form上放一个edit,一个button,一个ClientSocket 以下代码,错在哪里?procedure TForm1.Button1Click(Sender: TObject);
begin
  if ClientSocket.Active then ClientSocket.Close;
  ClientSocket.Host:=edit1.text;
  try
    ClientSocket.Open;
    if ClientSocket.Active then
    MessageDlg(‘Connect OK‘,mtInformation, [mbOk], 0);
  except
    on ESocketError do MessageDlg(‘error‘,mtInformation, [mbOk], 0);
  end;
  if ClientSocket.Active then ClientSocket.Close;
end;

解决方案 »

  1.   

    没错误,如果捕捉不到用on Exception do
       MessageDlg('error',mtInformation, [mbOk], 0);试试
      

  2.   

    try
      ...
    except
      on ex:exception do
        showmessage(ex.message);
    end;
      

  3.   

    我认为ClientSocket.Open里根本就没raise一个异常,那样自然就截获不到了。
    如果在程序运行中没有异常提示,那说明确实没有异常,因为Delphi程序始终包含在一个大的
    try...except..结构中的,ClientSocket.Open失败的信息也许使用另外的方式来返回。如果程序中有异常提示,那说明异常异常没有发生在你设置的try范围内。
      

  4.   

    异常也分静态和动态的,比如ABORT就是一个静态的异常,可是大家什么时候捕捉过它呢?
      

  5.   

    ClientSocket.Open,会花一些时间去寻找服务器,不会马上返回错误。所以在你的try..except..end之间不会得到错误。ClientSocket有OnConnect和OnError事件,你可以在里面显示提示信息。