Result :=True ;
 IdSMTP1.Host:='smtp.163.com';
 IdSmtp1.Port:=24;//这是我故意让程序出错
 statusbar1.SimpleText:='Connectting....';
 try
  IdSmtp1.Connect;//运行到此将会出现一个exception
 except
  on Exception do
   begin
    statusbar1.SimpleText:='Connected error';
    result:=false;
   end;
 end;
  if Result then
  statusbar1.SimpleText:='Connection ready'
  else
   statusbar1.SimpleText:='Connected error';
end;

解决方案 »

  1.   

    因为你函数最后重新赋值了,其实没有exit,函数体没有执行完!
      

  2.   

    function TfrmSendMail.CheckConnect: boolean;
    begin
     try
     IdSMTP1.Host:='smtp.163.com';
     IdSmtp1.Port:=24;//这是我故意让程序出错
     statusbar1.SimpleText:='Connectting....';
      IdSmtp1.Connect;//运行到此将会出现一个exception
     except
      on Exception do
       begin
        statusbar1.SimpleText:='Connected error';
        result:=false;
        exit;
       end;
     end;
     statusbar1.SimpleText:='Connection ready';
     result:=true;
    end;
      

  3.   

    on Exception do 有问题,
    应改为 出现异常的类型
      

  4.   

    to zhengxionghua:
    但是IdSMTP中并无onConnectionFailed事件...to  tccb(tccb);
    能否说详细点,最好给点例子。
      

  5.   

    你直接运行可执行文件试试,看看是不是你要的效果。如果是,就没有任何问题你的意思或许是在ide环境下发生已捕获过的异常吧。
      

  6.   

    实际上是程序跳出一条错误并且终止---------这条信息是你需要的错误吗?try
     IdSMTP1.Host:='smtp.163.com';
     IdSmtp1.Port:=24;//这是我故意让程序出错
     statusbar1.SimpleText:='Connectting....';
      IdSmtp1.Connect;//运行到此将会出现一个exception
     except
        statusbar1.SimpleText:='Connected error';  
        result:=false;
        exit;
     end;如果在 except END 中间出错,程序会立刻终止!一般,你即使不写TRY语句,出错也不会使整个程序崩溃,但如果在except END 中间出错,程序会立刻终止!所以建议你看看是不是这里错了。
      

  7.   

    我的意思是,我想捕获exception...
    我经过跟踪发现:
    except
      on Exception do
       begin
        statusbar1.SimpleText:='Connected error';
        result:=false;
        exit;
       end;
     end;
    这段代码根本就没有执行!!!这是怎么回事?
      

  8.   

    我没有IdSMTP控件,我用NMSMTP测试
    procedure TForm1.Button1Click(Sender: TObject);
    begin
     NMSMTP1.Host:='smtp.163.com';
     NMSMTP1.Port:=24;//这是我故意让程序出错
     try
      NMSMTP1.Connect;//运行到此将会出现一个exception
     except
      on e:Exception do
       begin
        showmessage(e.Message);
       end;
     end;
    end;
    在IDE中运行,结果IDE先报一个调试错误,然后出现一个对话框,完成正确。不知道兄弟你是什么原因?
      

  9.   

    try
     NMSMTP1.Host:='smtp.163.com';
     NMSMTP1.Port:=24;//这是我故意让程序出错
     NMSMTP11.Connect;//运行到此将会出现一个exception
     except
       on e:exception do
       showmessage(e.message); 
     end;
    我没有IdSmtp1控件,我用NMSMTP控件,在IDE中测试,先是IDE 报一个调试错误,然后出现一个对话框,完全正确。不知你是什么原因。。
      

  10.   

    Result :=True ;
     IdSMTP1.Host:='smtp.163.com';
     IdSmtp1.Port:=24;//这是我故意让程序出错
     statusbar1.SimpleText:='Connectting....';
     try
      IdSmtp1.Connect;//运行到此将会出现一个exception
     except
      on Exception do
       begin
        statusbar1.SimpleText:='Connected error';
        result:=false;
       end;
     end;
      if Result then
      statusbar1.SimpleText:='Connection ready'
      else
       statusbar1.SimpleText:='Connected error';
    end;